Removing unicode \u2026 like characters in a string in python2.7

ε祈祈猫儿з 提交于 2019-11-26 02:36:20

问题


I have a string in python2.7 like this,

 This is some \\u03c0 text that has to be cleaned\\u2026! it\\u0027s annoying!

How do i convert it to this,

This is some text that has to be cleaned! its annoying!

回答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!"


来源:https://stackoverflow.com/questions/15321138/removing-unicode-u2026-like-characters-in-a-string-in-python2-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!