How to do special encode in string ?

Duncan Booth me at privacy.net
Mon Jun 21 12:14:57 CEST 2004


"fowlertrainer at anonym.hu" <fowlertrainer at anonym.hu> wrote in 
news:mailman.88.1087811553.454.python-list at python.org:

> Encode("az állam én vagyok") -> "az \xe1llam \xe9n vagyok"
> 
> Decode("az \xe1llam \xe9n vagyok") -> "az állam én vagyok"
> 

>>> s = "az \xe1llam \xe9n vagyok"
>>> print s.decode('latin-1')
az állam én vagyok
>>> 

You want to use unicode strings if you have characters outside the ASCII 
range. The decode method on a byte string will let you convert it to a 
unicode string, and the encode method will let you convert it back to byte 
string.

The tricky bit is that you need to know the correct encoding to use as \xe1 
could mean different characters, but in this case it looks as though you 
meant latin-1.



More information about the Python-list mailing list