在python2.7中删除字符串中的unicode \u2026之类的字符[重复]
Posted
技术标签:
【中文标题】在python2.7中删除字符串中的unicode \\u2026之类的字符[重复]【英文标题】:Removing unicode \u2026 like characters in a string in python2.7 [duplicate]在python2.7中删除字符串中的unicode \u2026之类的字符[重复] 【发布时间】:2013-02-25 14:23:32 【问题描述】:我在python2.7中有一个这样的字符串,
This is some \u03c0 text that has to be cleaned\u2026! it\u0027s annoying!
如何转换成这个,
This is some text that has to be cleaned! its annoying!
【问题讨论】:
您要根据什么过滤字符?您只想保留 ASCII 码吗? @root,是的,我只想保留 ascii 【参考方案1】:Python 2.x
>>> s
'This is some \\u03c0 text that has to be cleaned\\u2026! it\\u0027s annoying!'
>>> print(s.decode('unicode_escape').encode('ascii','ignore'))
This is some text that has to be cleaned! it's annoying!
Python 3.x
>>> s = 'This is some \u03c0 text that has to be cleaned\u2026! it\u0027s annoying!'
>>> s.encode('ascii', 'ignore')
b"This is some text that has to be cleaned! it's annoying!"
【讨论】:
就是这样打印出来的 也很好奇你为什么写\` but not
`,你的代码输出的不是一样吗?
这也会去除ü, ä, ö
等字符,这在大多数情况下是不需要的。
@BurhanKhalid,在你的代码中's'是一个字符串?如果是,那么我是否需要导入一些包来获取解码和编码辅助方法?
请阅读this和this。以上是关于在python2.7中删除字符串中的unicode \u2026之类的字符[重复]的主要内容,如果未能解决你的问题,请参考以下文章
python2.7 处理unicode和ascii字符串混用问题
即使一切都是 unicode(python 2.7),ascii 解码错误
python2.7运行出现的Warning: UnicodeWarning: Unicode equal comparison failed to convert both arguments to
有效地将字符串转换为 python 2.7 的 unicode
Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'