src/cm/utils/string_utils.py
author rbernard
Sat, 13 Feb 2010 12:50:06 +0100
changeset 183 f1c6f8fadfef
parent 175 4f072edc51a1
child 236 725653080973
permissions -rw-r--r--
(re) edit : prevent tab change on cancel message + confirm_all_removed_dlg renamed (bug fix)

import chardet
import re

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

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