/
unicode_decode.py
68 lines (56 loc) · 1.57 KB
/
unicode_decode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
# coding=utf-8
from __future__ import unicode_literals
import sys
try:
import cchardet as chardet
except ImportError:
try:
import chardet
except ImportError:
pass
if sys.version_info[0] == 3:
binary_type = bytes
text_type = str
else:
# noinspection PyUnresolvedReferences
text_type = unicode
binary_type = str
def unicode_decode(content):
r"""
用多种方式尝试解码二进制内容为unicode文本
copy from `unicode_decode.py`
:return: tuple(编码, 解码后的unicode)
:rtype: (str, bytes)
>>> unicode_decode("简体中文UTF8汉字".encode("utf8"))
('UTF-8', '简体中文UTF8汉字')
>>> unicode_decode("简体中文GBK汉字".encode("gbk"))
('GB18030', '简体中文GBK汉字')
>>> unicode_decode(b'\xfa\xfb\xfc')
Traceback (most recent call last):
...
UnicodeError: unable to decode b'\xfa\xfb\xfc'
"""
try:
return "UTF-8", content.decode("UTF-8")
except:
pass
try:
return "GB18030", content.decode("GB18030")
except:
pass
try:
encoding = chardet.detect(content)["encoding"]
return encoding, content.decode(encoding)
except:
pass
raise UnicodeError("unable to decode {}".format(repr(content[:32])))
def ensure_unicode(content):
if content is None:
return content
if isinstance(content, text_type):
return content
elif isinstance(content, binary_type):
_, uni = unicode_decode(content)
return uni
return content