src/cm/utils/string_utils.py
changeset 149 0f2c5744b39b
parent 119 5e8dda1b7631
child 175 4f072edc51a1
equal deleted inserted replaced
148:03106bfa4845 149:0f2c5744b39b
       
     1 import chardet
       
     2 
       
     3 def to_unicode(input):
       
     4     if type(input) == str:
       
     5         res = None
       
     6         for encoding in [chardet.detect(input)['encoding'], 'utf8', 'latin1']:
       
     7             try:
       
     8                 res = unicode(input, encoding)
       
     9                 break;
       
    10             except UnicodeDecodeError:
       
    11                 pass
       
    12         if not res:
       
    13             raise Exception('UnicodeDecodeError: could not decode')
       
    14         return res
       
    15     return input