我正在尝试从Python中的字符串中删除某些字符。我有一个需要删除的字符列表或字符范围,用十六进制表示如下:
- "0x00:0x20"
- "0x7F:0xA0"
- "0x1680"
- "0x180E"
- "0x2000:0x200A"
我将此列表转换为如下所示的正则表达式:
re.sub(u'[\x00-\x20 \x7F-\xA0 \x1680 \x180E \x2000-\x200A]', ' ', my_str)
然而,当我有\x2000-\x200A
时,我得到了一个错误。你知道吗
我发现Python实际上并没有将u'\x2000'
解释为字符:
>>> '\x2000'
' 00'
它把它当作“x20”(一个空格)以及它后面的任何东西:
>>> '\x20blah'
' blah'
x2000是有效的unicode字符: http://www.unicodemap.org/details/0x2000/index.html
我希望Python以这种方式处理它,这样我就可以使用re
将它从字符串中删除。你知道吗
另外,我想知道从字符串中删除这些字符的另一种方法。你知道吗
谢谢你的帮助。谢谢!你知道吗
从文档(https://docs.python.org/2/howto/unicode.html):
在unicode字符串中,需要指定unicode字符(
\uNNNN
而不是\xNNNN
)。以下工作:相关问题 更多 >
编程相关推荐