How to do special encode in string ?

Scott David Daniels Scott.Daniels at Acm.Org
Wed Jun 23 11:45:49 EDT 2004


fowlertrainer at anonym.hu wrote:

> Hi !
> 
> I'm hungarian, we use special characters like:
> á - a'
> ő -o"
> 
> etc.
> 
> I want to encode this characters to in config file I see these
> characters as \nnn format.
> And I want to decode it automatically with python.
> 
> How to I do it without write complex converter tool ?
> 
> Thanx for it:
> FT
> 
> Example:
> Encode("az állam én vagyok") -> "az \xe1llam \xe9n vagyok"
> 
> Decode("az \xe1llam \xe9n vagyok") -> "az állam én vagyok"
> 
> 
What Christopher Koppler was telling you was roughly this:

 >>> "az állam én vagyok".decode('iso8859-2')
u'az \xe1llam \xe9n vagyok'

 >>> u'az \xe1llam \xe9n vagyok'.encode('iso8859-2')
'az \xe1llam \xe9n vagyok'

The way to think of this stuff is:
unicode strings are strings of _characters_, "normal" strings
are strings of _bytes_.  You cannot translate strings of bytes
into anything with identifying what encoding was used to create
those bytes.  You cannot convert unicode strings (strings of
characters) to bytes without specifying what encoding to use.

HTH

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list