5

I have been searching for hours now to find a way to fully reverse the result of a str.encode-call like this:

"testäch基er".encode("cp1252", "backslashreplace")

The result is

b'test\xe4ch\\u57faer'

now i want to convert it back with

b'test\xe4ch\\u57faer'.decode("cp1252")

and I get

'testäch\\u57faer'

So how do I get my 基 back? I'm getting nearly there by using decode("unicode-escape") instead (it would work for this example), but that assumes bytes encoded with iso8859-1 not cp1252, so any characters between 80 and 9F would be wrong.

1
  • It would be useful to give some context. What are you doing with those bytes? Displaying them? Or storing them in a file?
    – roeland
    Jul 9, 2015 at 23:36

2 Answers 2

5

Well...

>>> b'test\xe4ch\\u57faer'.decode('unicode-escape')
'testäch基er'

But backslashreplace->unicode-escape is not a consistent round trip. If you have backslashes in the original string, they won't get encoded by backslashreplace but they will get decoded by unicode-escape, and replaced with unexpected characters.

>>> '☃ \\u2603'.encode('cp1252', 'backslashreplace').decode('unicode-escape')
'☃ ☃'

There is no way to reliably reverse the encoding of string that has been encoded with an errors fallback such as backslashreplace. That's why it's a fallback, if you could consistently encode and decode to it, it would have been a real encoding.

1
  • I'm now using the quote and unquote functions from the urllib.parse module to achieve a safe and standards-compliant escaping of unicode characters. See accepted answer.
    – Bachsau
    Feb 1, 2022 at 23:57
2

I was still very new to Python when I asked this question. Now I understand that these fallback mechanisms are just meant for handling unexpected errors, not something to save and restore data. If you really need a simple and reliable way to encode single unicode characters in ASCII, have a look at the quote and unquote functions from the urllib.parse module.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.