src/cm/utils/string.py
author raph
Wed, 27 Jan 2010 16:18:30 +0100
changeset 130 f257ad33bed5
parent 119 5e8dda1b7631
permissions -rw-r--r--
model change+migration (add id_key to comment): run ./bin/django migrate

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