gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:44:03 26 4
gpt4 key购买 nike

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

最佳答案

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

关于python - 在python2.7中删除字符串中的unicode\u2026之类的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15321138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com