Python字符串删除十六进制代码,例如'总统特朗普\\ xe2 \\ x80 \\ x99s'

时间:2017-06-04 11:19:25

标签: python string hex

我搜索了一段时间但却找不到有用的东西。 我尝试在Paython列表中的字符串中删除这些十六进制代码,但无法弄清楚如何执行此操作。它们是字符串格式! 除了我的数据(特朗普Twitter提取):

tweets[7]
'rt @ lindseygrahamsc : i support president trump\\xe2\\x80\\x99s desire to reenter the paris accord after the agreement becomes a better deal for america\\xe2\\x80\\xa6'

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试这种方式,它只适用于子字符串' \\x**':

import re
tweets = 'rt @ lindseygrahamsc : i support president trump\\xe2\\x80\\x99s desire to reenter the paris accord after the agreement becomes a better deal for america\\xe2\\x80\\xa6'
re.sub(r'(\\x(.){2})', '',tweets)

输出:

'rt @ lindseygrahamsc : i support president trumps desire to reenter the paris accord after the agreement becomes a better deal for america'