在python2.7中删除字符串中的unicode \u2026之类的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15321138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:48:24  来源:igfitidea点击:

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

pythonpython-2.7python-unicodeunicode-escapes

提问by Sandeep Raju Prabhakar

I have a string in python2.7 like this,

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

 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!

采纳答案by Burhan Khalid

Python 2.x

蟒蛇 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

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