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

San*_*kar 37 python python-2.7 unicode-escapes python-unicode

我在python2.7中有一个像这样的字符串,

 This is some \u03c0 text that has to be cleaned\u2026! it\u0027s annoying!
Run Code Online (Sandbox Code Playgroud)

我怎么把它转换成这个,

This is some text that has to be cleaned! its annoying!
Run Code Online (Sandbox Code Playgroud)

Bur*_*lid 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!
Run Code Online (Sandbox Code Playgroud)

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!"
Run Code Online (Sandbox Code Playgroud)

  • 这就是它的印刷方式 (2认同)
  • 这还会去除诸如ü,ä,ö等字符,这在大多数情况下是不希望的。 (2认同)