在python2.7中删除字符串中的unicode \ u2026喜欢的字符[重复]

发布于 2021-01-29 14:55:27

这个问题已经在这里有了答案

用单个空格替换非ASCII字符 (7个答案)

如何从Python中的字符串中删除\ xa0? (13个回答)

4个月前关闭。

我在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!
关注者
0
被浏览
115
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看