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

Troels Therkelsen

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"

The easiest way is probably just to use repr/eval, like this:

Python 2.3.4 (#1, Jun 4 2004, 19:45:32)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.az állam én vagyok

Not that repr() puts a set of '' around the string and eval() needs these in
order to 'parse' the string.

Hope this helps!

Regards,

Troels Therkelsen
 
S

Scott David Daniels

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:
u'az \xe1llam \xe9n vagyok'
'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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top