我遇到过一个情况,需要将一串字符转换为python中的字符串。
s = "\\x80\\x78\\x07\\x00\\x75\\xb3"
print s #gives: \x80\x78\x07\x00\x75\xb3
我想要的是,给定字符串 s,我可以获得 s 中的真实字符存储。在本例中是“\x80、\x78、\x07、\x00、\x75 和 \xb3”(类似这样)�xu�。
您可以使用string-escape编码 https://docs.python.org/2/library/codecs.html#python-specific-encodings(Python 2.x):
>>> s = "\\x80\\x78\\x07\\x00\\x75\\xb3"
>>> s.decode('string-escape')
'\x80x\x07\x00u\xb3'
Use unicode-escape编码 https://docs.python.org/3/library/codecs.html#python-specific-encodings(在Python 3.x中,需要先转换为字节):
>>> s.encode().decode('unicode-escape')
'\x80x\x07\x00u³'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)