Is there a way to get utf-8 out of a Unicode string?

Gerrit Holl gerrit at
Mon Oct 30 08:31:52 CET 2006


On 2006-10-30 08:25:41 +0100, thebjorn wrote:
>   def unfk(s):
>       return eval(repr(s)[1:]).decode('utf-8')
> i.e. chopping off the u in the repr of a unicode string, and relying on
> eval to interpret the \xHH sequences.
> Is there a less hack'ish way to do this?

Slightly lack hackish:

return ''.join(chr(ord(c)) for c in s)


More information about the Python-list mailing list