hex value in string back to real hex value

Peter Otten __peter__ at web.de
Tue Sep 7 13:45:53 EDT 2004


jack wrote:

> I get in a program an hexa value codes in a string for example \xe7
> which correspond to a french character.
> 
> The string would be "\xe7t\xe7" and I want to write in a file été and
> not \xe7t\xe7.
 
"\xe7t\xe7" _is_ the same as "été" if you are using e. g. the ISO-8859-1
encoding, so chances are you will see été if you open the file in a text
editor without the need of prior conversions.

But I may be misunderstanding you and you really have "\\xe7t\\xe7" - in
that case you can use de/encode to switch between the two representations:

>>> "été".encode("string_escape")
'\\xe9t\\xe9'
>>> _.decode("string_escape")
'\xe9t\xe9'
>>> print _
été

(In the interpreter the result of the previous calculation is assigned to _
if it is not None)

Peter





More information about the Python-list mailing list