Mario Orlandi's Snippets

04/03/2011

fix encoding

Filed under: Uncategorized — Tags: , — morlandi @ 15:03
def fix_encoding(text):
    """ force utf-8 encoding """
    encodings = ('iso-8859-15','utf-8','ascii')
    success = False
    for encoding in encodings:
        try:
            utext = text.decode(encoding)
            success = True
            break
        except:
            success = False
    if success:
        return utext.encode('utf-8')
    return text

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.