There are many questions about utf-8 > unicode conversion, but I still haven't found answer for my issue.
Lets have strings like this:
a = "Je-li pro za\\xc5\\x99azov\\xc3\\xa1n\\xc3\\xad"
Python 3.6 understands this string like Je-li pro za\xc5\x99azov\xc3\xa1n\xc3\xad. I need to convert this utf-8-like string to unicode representation. The final result should be Je-li pro zařazování.
With a.decode("utf-8")
I get AttributeError: 'str' object has no attribute 'decode', because Python means the object is already decoded.
If I convert it to bytes first with bytes(a, "utf-8")
, the backslashes are doubled only and .decode("utf-8")
returns it to my current a
again.
How to get unicode string Je-li pro zařazování from this a
?
You have to encode/decode 4 times to get the desired result:
Try it online!
Besides, consider telling your target program (data source) to give different output format (byte array or base64 encoded, for example), if you can.
The unsafe-but-shorter way:
Try it online!
There are
ast.literal_eval
, but it may not worth using here.