| author | raph |
| Mon, 25 Jan 2010 11:34:22 +0100 | |
| changeset 119 | 5e8dda1b7631 |
| permissions | -rw-r--r-- |
| 119 | 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 |