character encoding conversion

Max M maxm at mxm.dk
Mon Dec 13 04:48:14 EST 2004


Christian Ergh wrote:

A smiple way to try out different encodings in a given order:

# -*- coding: latin-1 -*-

def get_encoded(st, encodings):
     "Returns an encoding that doesn't fail"
     for encoding in encodings:
         try:
             st_encoded = st.decode(encoding)
             return st_encoded, encoding
         except UnicodeError:
             pass


st = 'Test characters æøå ÆØÅ'
encodings = ['utf-8', 'latin-1', 'ascii', ]
print get_encoded(st, encodings)

     (u'Test characters \xe6\xf8\xe5 \xc6\xd8\xc5', 'latin-1')

-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list