src/cm/utils/string_utils.py
author Kirill Miazine <km@krot.org>
Sat, 06 Feb 2010 16:16:24 +0100
changeset 152 18e02f70d233
parent 149 0f2c5744b39b
child 175 4f072edc51a1
permissions -rw-r--r--
Norwegian update

import chardet

def to_unicode(input):
    if type(input) == str:
        res = None
        for encoding in [chardet.detect(input)['encoding'], 'utf8', 'latin1']:
            try:
                res = unicode(input, encoding)
                break;
            except UnicodeDecodeError:
                pass
        if not res:
            raise Exception('UnicodeDecodeError: could not decode')
        return res
    return input