src/cm/utils/string_utils.py
author raph
Tue, 30 Mar 2010 14:13:45 +0200
changeset 236 725653080973
parent 175 4f072edc51a1
permissions -rw-r--r--
fix error if no encoding is detected

import chardet
import re

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

# strip carriage returns
def strip_cr(input):
    return re.sub('\r\n|\r|\n', '\n', input)