简体   繁体   English

使用Python将“\ x”替换为文本中的“0x”

[英]Replace “\x” with “0x” in a text using Python

I have a text that contains \\x and I want to replace this pair with 0x . 我有一个包含\\x的文本,我想用0x替换这对。 I used the below Python commands, but all failed: 我使用了以下Python命令,但都失败了:

>>> text= '\x1bs\x1b\x01Z\xa2\x8d\xa2^\xb9*d\x08\x10&B\xb1z\xd4\xa91\xa3D
\xaf\xa1\x9a\x94\x8c\xd3\xb2r\x80\xc3\xb7)\xd8\x1bi\x80\x81\x02\x04\x08\x10@
\x81}0\xa8J\x95\x02E\x08\x10 @\x81\x02\x04\x08\x10 @\x81\x02\x04\x08\x10@
\x9f+\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x1b\x02^\xd3Q\xcc\xd70\x0eB\x88A\x1chB\x1bL\x81\xadC\x00
\x84!\rr(\x07\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x03
\x00?\x1c\x1b0'
>>>
>>>
>>> text.replace('\x',' 0x')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape
>>>
>>>
>>> text.replace(u'\x',' 0x')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape
>>>
>>>
>>> text.replace(r'\x',' 0x')
'\x1bs\x1b\x01Z¢\x8d¢^¹*d\x08\x10&B±zÔ©1£D¯¡\x9a\x94\x8cÓ²r\x80÷)Ø\x1bi\x80
\x81\x02\x04\x08\x10 @\x81}0¨J\x95\x02E\x08\x10 @\x81\x02\x04\x08\x10 @\x81
\x02\x04\x08\x10@\x9f+\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x02^ÓQÌ×0\x0eB\x88A\x1chB\x1bL\x81\xadC
\x00\x84!\rr(\x07ÿ\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x03
\x00?\x1c\x1b0'
>>>
>>>
>>> text.replace('\\x',' 0x')
'\x1bs\x1b\x01Z¢\x8d¢^¹*d\x08\x10&B±zÔ©1£D¯¡\x9a\x94\x8cÓ²r\x80÷)Ø\x1bi\x80
\x81\x02\x04\x08\x10 @\x81}0¨J\x95\x02E\x08\x10 @\x81\x02\x04\x08\x10 @\x81
\x02\x04\x08\x10@\x9f+\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x02^ÓQÌ×0\x0eB\x88A\x1chB\x1bL\x81\xadC
\x00\x84!\rr(\x07ÿ\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x03
\x00?\x1c\x1b0'
>>> 

What's wrong? 怎么了? What shall I do? 我该怎么办?

Note that I am using Python 3.4. 请注意,我使用的是Python 3.4。

Thanks in advance. 提前致谢。

text= repr('''\x1bs\x1b\x01Z\xa2\x8d\xa2^\xb9*d\x08\x10&B\xb1z\xd4\xa91\xa3D
\xaf\xa1\x9a\x94\x8c\xd3\xb2r\x80\xc3\xb7)\xd8\x1bi\x80\x81\x02\x04\x08\x10@
\x81}0\xa8J\x95\x02E\x08\x10 @\x81\x02\x04\x08\x10 @\x81\x02\x04\x08\x10@
\x9f+\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x1b\x02^\xd3Q\xcc\xd70\x0eB\x88A\x1chB\x1bL\x81\xadC\x00
\x84!\rr(\x07\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x03
\x00?\x1c\x1b0''')

print text.replace("\\x","0x")

Use representation of the original string and then replace 使用原始字符串的表示,然后替换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM