从Python3中的unicode文件文件中删除一个unicode范围

时间:2011-02-03 04:14:25

标签: unicode python-3.x

我有一个Unicode文本文件,包括范围q到r和s到t。我想将范围s移到t(中文),将q留给r(用英文)。

我怎么能在Python3中做到这一点?

1 个答案:

答案 0 :(得分:2)

使用字符串翻译方法。引用3.1.3 Std Lib doc:

str.translate(map) 
Return a copy of the s where all characters have been mapped through the map 
which must be a dictionary of Unicode ordinals (integers) to Unicode ordinals, 
strings or None. Unmapped characters are left untouched. Characters mapped to 
None are deleted.

You can use str.maketrans() to create a translation map from 
character-to-character mappings in different formats.