[英]Python: unescape “\xXX”
我有一個帶有轉義數據的字符串
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'
你可以使用'unicode_escape'編解碼器:
>>> '\\x50\\x51'.decode('unicode_escape')
u'PQ'
或者,'string-escape'將為您提供經典的Python 2字符串(Python 3中的字節):
>>> '\\x50\\x51'.decode('string_escape')
'PQ'
escaped_data.decode('unicode-escape')
幫助嗎?
嘗試:
eval('"' + raw_data + '"')
它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.