I have a text that contains \x and I want to replace this pair with 0x. I used the below Python commands, but all failed:
>>> 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With