Python:将\ xhh转换为字符

时间:2015-09-24 03:17:39

标签: python string

我的文字如下:

"\x91It will have to be paid for,\x92 they said. \x91It isn\x92t natural, and\ntrouble will come of it!"

我想将其转换为:

"'It will have to be paid for,' they said. 'It isn't natural, and\ntrouble will come of it!"

3 个答案:

答案 0 :(得分:0)

data = r"\x91It will have to be paid for,\x92 they said. \x91It isn\x92t natural, and\ntrouble will come of it!"
print data.replace('\\x91',"'").replace('\\x92',"'")

答案 1 :(得分:0)

\x91表示LEFT SINGLE QUOTATION MARK\x92表示代码页1252中的RIGHT SINGLE QUOTATION MARK

您可以通过以下方式获取原始文本:

text = "\x91It will have to be paid for,\x92 they said. \x91It isn\x92t natural, and\ntrouble will come of it!"
print unicode(text, 'cp1252')

答案 2 :(得分:0)

Unidecode

>>> unidecode.unidecode("\x91It will have to be paid for,\x92 they said. \x91It isn\x92t natural, and\ntrouble will come of it!".decode('cp1252'))
"'It will have to be paid for,' they said. 'It isn't natural, and\ntrouble will come of it!"