在python2.7中删除Unicode \ u2026像字符串中的字符 [英] Removing unicode \u2026 like characters in a string in python2.7

查看:486
本文介绍了在python2.7中删除Unicode \ u2026像字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a string in python2.7 like this,

 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!

推荐答案

Python 2.x

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

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

这篇关于在python2.7中删除Unicode \ u2026像字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆