conversion to and from unicode strings

Gerhard Häring gh at ghaering.de
Mon Oct 27 09:59:28 CET 2008


abhishek wrote:
> hello group,
>   i want to represent and store a string u'\x00\x07\xa7' as
> '\x00\x07\xa7'. any ideas on how to achieve this.

You want to store it in the form of the repr() of the string? It is 
possible to use repr() to get a bytestring to store and to use eval() to 
create a unicode string of that bytestring again. But that's just bad.

It's much better to use a encoding that can represent all Unicode 
characters like UTF-8.

 >>> s = u'\x00\x07\xa7'
 >>> bytestr = s.encode("utf-8")
 >>> bytestr
'\x00\x07\xc2\xa7'
 >>> out_str = unicode(bytestr, "utf-8")
 >>> out_str == s
True

-- Gerhard




More information about the Python-list mailing list