UTF-8 to unicode or latin-1 (and yes, I read the FAQ)

NoelByron at gmx.net NoelByron at gmx.net
Thu Oct 19 05:16:51 EDT 2006


Hi!

I'm struggling with the conversion of a UTF-8 string to latin-1. As far
as I know the way to go is to decode the UTF-8 string to unicode and
then encode it back again to latin-1?

So I tried:

'K\xc3\xb6ni'.decode('utf-8')     # 'K\xc3\xb6ni' should be 'König',
contains a german 'umlaut'

but failed since python assumes every string to decode to be ASCII?

How can I convert this string to latin-1?

How would you write a function like:

def encode_string(string, from_encoding, to_encoding):
  #????

Best regards,
Noel




More information about the Python-list mailing list