src/cm/utils/string_utils.py
author rbernard
Tue, 09 Feb 2010 22:20:08 +0100
changeset 155 f436e2fef076
parent 149 0f2c5744b39b
child 175 4f072edc51a1
permissions -rw-r--r--
Merge with e4746a10edf5ac1bcea7348d228fb35bebf93657

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