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 .decode
a which decodes the sequence as wanted:
>>> a.decode()
'ä\\n-\\t-\\"foo\\"'
But it does not un-escape. Then I found 'unicode_escape'
and I got
>>> print(a.decode('unicode_escape'))
ä
- -"foo"
Is there a way to decode and unescape the given sequence with a builtin method (i.e. without having to .replace('\\n', '\n').replace(...)
)?
It would be also interesting to know how I can revert this operation (i.e. getting the same byte sequence from the translated result).