gpt4 book ai didi

python :转义 "\xXX"

转载 作者:太空狗 更新时间:2023-10-29 17:26:42 30 4
gpt4 key购买 nike

我有一个带有转义数据的字符串

escaped_data = '\\x50\\x51'
print escaped_data # gives '\x50\x51'

什么 Python 函数会对其进行反转义以便我得到

raw_data = unescape( escaped_data)
print raw_data # would print "PQ"

最佳答案

您可以使用string-escape 进行解码。

>>> escaped_data = '\\x50\\x51'
>>> escaped_data.decode('string-escape')
'PQ'

Python 3.0没有 string-escape,但您可以使用 unicode_escape

来自 bytes 对象:

>>> escaped_data = b'\\x50\\x51'
>>> escaped_data.decode("unicode_escape")
'PQ'

来自 Unicode str 对象:

>>> import codecs
>>> escaped_data = '\\x50\\x51'
>>> codecs.decode(escaped_data, "unicode_escape")
'PQ'

关于 python :转义 "\xXX",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10944907/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com