how do I enforce unicode escape in python?
I have the unicode string u'aä' or u'a\u00e4' and I want it to be represented as a\u00e4. The solution suggested here doesn’t work for me:
>>> repr(u'a\u00e4')
"u'a\\xe4'"
From my understanding the following attempt should be the correct solution (but isn’t):
>>> u'a\u00e4'.encode('unicode-escape')
'a\\xe4'
I tried it with python 2.6 and 2.7 on OS X and with 2.6 on Debian Squeeze.
Thanks for your time.
Unicode characters below 256 are always encoded with
\xin theirrepr()esentation, as it is the least space-consuming.Nevertheless, they are unicode because of the
u'at the start. It is just a matter of representation.If you want it nevertheless, you could do
.