How to do special encode in string ?

F

fowlertrainer

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"
 
D

Duncan Booth

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

Decode("az \xe1llam \xe9n vagyok") -> "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.
 
C

Christopher Koppler

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.

For Hungarian long umlauts, you'll want to use latin-2 (or iso8859-2).
 
C

Chris King

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

The functions you want are str.encode and str.decode:
"az llam n vagyok".encode("string_escape") -> "az \xe1llam \xe9n
vagyok"
"az \xe1llam \xe9n vagyok".decode("string_escape") -> "az llam n
vagyok"

If you choose to use Unicode strings instead, use the "unicode_escape"
codec instead of the "string_escape" codec.

A list of the standard encodings is available at
http://docs.python.org/lib/node127.html if you need with some other
format (rot13 is my personal favourite :p).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top