【发布时间】:2011-02-03 04:14:25
【问题描述】:
我有一个由 q 到 r 和 s 到 t 范围组成的 Unicode 文本文件。我想删除范围 s 到 t(中文),留下 q 到 r(英文)。
如何在 Python3 中做到这一点?
【问题讨论】:
-
在这种情况下,什么是“范围”? “范围 q 到 r”实际上没有任何意义,因为 q 不是范围,而是一个字母。 :)
标签: unicode python-3.x
我有一个由 q 到 r 和 s 到 t 范围组成的 Unicode 文本文件。我想删除范围 s 到 t(中文),留下 q 到 r(英文)。
如何在 Python3 中做到这一点?
【问题讨论】:
标签: unicode python-3.x
使用字符串翻译方法。引用 3.1.3 Std Lib 文档:
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.
【讨论】: