Python 3.4: 将列表中的“0x”替换为“\x”
Python 3.4: replace "0x" with "\x" in a List
您好,我想在此列表中用“\x”替换“0x”:
['0x50', '0x0', '0x0', '0x0']
我用这个列表理解试过了:
result = ['0x50', '0x0', '0x0', '0x0']
result = [x.replace("0x","\x") for x in result]
但是它给了我这个错误:
(unicode error)"unicodeescape" codex cant decode byte in position 0-1: truncated \xXX escape
现在如何更改“0x”和“\x”?
将其设为原始字符串,如
>>> l = ['0x50', '0x0', '0x0', '0x0']
>>> [i.replace('0x',r'\x') for i in l]
['\x50', '\x0', '\x0', '\x0']
或双转义 \
如
>>> [i.replace('0x','\x') for i in l]
['\x50', '\x0', '\x0', '\x0']
您好,我想在此列表中用“\x”替换“0x”:
['0x50', '0x0', '0x0', '0x0']
我用这个列表理解试过了:
result = ['0x50', '0x0', '0x0', '0x0']
result = [x.replace("0x","\x") for x in result]
但是它给了我这个错误:
(unicode error)"unicodeescape" codex cant decode byte in position 0-1: truncated \xXX escape
现在如何更改“0x”和“\x”?
将其设为原始字符串,如
>>> l = ['0x50', '0x0', '0x0', '0x0']
>>> [i.replace('0x',r'\x') for i in l]
['\x50', '\x0', '\x0', '\x0']
或双转义 \
如
>>> [i.replace('0x','\x') for i in l]
['\x50', '\x0', '\x0', '\x0']