Skip to content Skip to sidebar Skip to footer

How Combine 'utf-8' And 'unicode_escape' To Correctly Decode B'\xc3\xa4\\n-\\t-\\"foo\\"'?

I have a library that gives me encoded and escaped byte sequences like this one: a=b'\xc3\xa4\\n-\\t-\\'foo\\'' Which I want to translate back to: ä - -'foo' I tried to just .

Solution 1:

There is a way to somehow do what I want and I can almost go the other way, too but in my eyes it's ugly and incomplete, so I hope it's not the best option I have:

>>> import codecs
>>> decoded = codecs.escape_decode(a)[0].decode()
>>> print(decoded)
ä
-   -"foo"
>>> reencoded = codecs.escape_encode(decoded.encode())
>>> print(reencoded)
(b'\\xc3\\xa4\\n-\\t-"foo"', 11)      <--- qotes are note escaped

Post a Comment for "How Combine 'utf-8' And 'unicode_escape' To Correctly Decode B'\xc3\xa4\\n-\\t-\\"foo\\"'?"