删除python2.7中字符串中的unicode \ u2012字符

时间:2013-03-10 10:17:19

标签: python python-2.7 python-unicode unicode-escapes

我在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!

1 个答案:

答案 0 :(得分:82)

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