vendredi 20 février 2015

Is converting Python unicode by casting to str reversible?

The proper way to convert a unicode string u to a (byte)string in Python is by calling u.encode(someencoding). Unfortunately, I didn't know that before and I had used str(u) for conversion. In particular, I called str(u) to coerce u to be a string so that I can make it a valid shelve key (which must be a str). Since I didn't encounter any UnicodeEncodeError, I wonder if this process is reversible/lossless. That is, can I do u = str(converted_unicode) (or u = bytes(converted_unicode) in Python 3) to get the original u?


Aucun commentaire:

Enregistrer un commentaire