简体   繁体   English

Python 3.4:在列表中将“ 0x”替换为“ \\ x”

[英]Python 3.4: replace “0x” with “\x” in a List

Hi I want to replace "0x" with "\\x" in this list: 嗨,我想在此列表中将“ 0x”替换为“ \\ x”:

['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? 现在如何更改“ 0x”和“ \\ x”?

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']

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

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