How to convert a raw string r'\xdd' to '\xdd' more gracefully?

Jach Feng jfong at ms4.hinet.net
Thu Dec 8 03:56:42 EST 2022


Jach Feng 在 2022年12月7日 星期三上午10:23:20 [UTC+8] 的信中寫道:
> s0 = r'\x0a' 
> At this moment it was done by 
> 
> def to1byte(matchobj): 
> ....return chr(int('0x' + matchobj.group(1), 16)) 
> s1 = re.sub(r'\\x([0-9a-fA-F]{2})', to1byte, s0) 
> 
> But, is it that difficult on doing this simple thing? 
> 
> --Jach
I find another answer on the web.

>>> s0 = r'\x0a'
>>> s0.encode('Latin-1').decode('unicode-escape')
'\n'


More information about the Python-list mailing list