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

MRAB python at mrabarnett.plus.com
Tue Dec 6 22:01:12 EST 2022


On 2022-12-07 02:23, Jach Feng wrote:
> 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?
> 
You could try this:

 >>> s0 = r'\x0a'
 >>> ast.literal_eval('"%s"' % s0)
'\n'



More information about the Python-list mailing list