Reputation: 153
Hi I want to replace "0x" with "\x" in this list:
['0x50', '0x0', '0x0', '0x0']
I tried it with this list comprehension:
result = ['0x50', '0x0', '0x0', '0x0']
result = [x.replace("0x","\x") for x in result]
But it gives me this Error:
(unicode error)"unicodeescape" codex cant decode byte in position 0-1: truncated \xXX escape
How can I change "0x" and "\x" now?
Upvotes: 0
Views: 1626
Reputation: 52161
Make it a raw string as in
>>> l = ['0x50', '0x0', '0x0', '0x0']
>>> [i.replace('0x',r'\x') for i in l]
['\\x50', '\\x0', '\\x0', '\\x0']
Or double-escape \
as in
>>> [i.replace('0x','\\x') for i in l]
['\\x50', '\\x0', '\\x0', '\\x0']
Upvotes: 5