| changeset 119 | 5e8dda1b7631 |
| 118:75d94dd14511 | 119:5e8dda1b7631 |
|---|---|
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 |