pyxllib 0.0.43__py3-none-any.whl → 0.3.197__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. pyxllib/__init__.py +9 -2
  2. pyxllib/algo/__init__.py +8 -0
  3. pyxllib/algo/disjoint.py +54 -0
  4. pyxllib/algo/geo.py +541 -0
  5. pyxllib/{util/mathlib.py → algo/intervals.py} +172 -36
  6. pyxllib/algo/matcher.py +389 -0
  7. pyxllib/algo/newbie.py +166 -0
  8. pyxllib/algo/pupil.py +629 -0
  9. pyxllib/algo/shapelylib.py +67 -0
  10. pyxllib/algo/specialist.py +241 -0
  11. pyxllib/algo/stat.py +494 -0
  12. pyxllib/algo/treelib.py +149 -0
  13. pyxllib/algo/unitlib.py +66 -0
  14. pyxllib/autogui/__init__.py +5 -0
  15. pyxllib/autogui/activewin.py +246 -0
  16. pyxllib/autogui/all.py +9 -0
  17. pyxllib/autogui/autogui.py +852 -0
  18. pyxllib/autogui/uiautolib.py +362 -0
  19. pyxllib/autogui/virtualkey.py +102 -0
  20. pyxllib/autogui/wechat.py +827 -0
  21. pyxllib/autogui/wechat_msg.py +421 -0
  22. pyxllib/autogui/wxautolib.py +84 -0
  23. pyxllib/cv/__init__.py +1 -11
  24. pyxllib/cv/expert.py +267 -0
  25. pyxllib/cv/{imlib.py → imfile.py} +18 -83
  26. pyxllib/cv/imhash.py +39 -0
  27. pyxllib/cv/pupil.py +9 -0
  28. pyxllib/cv/rgbfmt.py +1525 -0
  29. pyxllib/cv/slidercaptcha.py +137 -0
  30. pyxllib/cv/trackbartools.py +163 -49
  31. pyxllib/cv/xlcvlib.py +1040 -0
  32. pyxllib/cv/xlpillib.py +423 -0
  33. pyxllib/data/__init__.py +0 -0
  34. pyxllib/data/echarts.py +240 -0
  35. pyxllib/data/jsonlib.py +89 -0
  36. pyxllib/{util/oss2_.py → data/oss.py} +11 -9
  37. pyxllib/data/pglib.py +1127 -0
  38. pyxllib/data/sqlite.py +568 -0
  39. pyxllib/{util → data}/sqllib.py +13 -31
  40. pyxllib/ext/JLineViewer.py +505 -0
  41. pyxllib/ext/__init__.py +6 -0
  42. pyxllib/{util → ext}/demolib.py +119 -35
  43. pyxllib/ext/drissionlib.py +277 -0
  44. pyxllib/ext/kq5034lib.py +12 -0
  45. pyxllib/{util/main.py → ext/old.py} +122 -284
  46. pyxllib/ext/qt.py +449 -0
  47. pyxllib/ext/robustprocfile.py +497 -0
  48. pyxllib/ext/seleniumlib.py +76 -0
  49. pyxllib/{util/tklib.py → ext/tk.py} +10 -11
  50. pyxllib/ext/unixlib.py +827 -0
  51. pyxllib/ext/utools.py +351 -0
  52. pyxllib/{util/webhooklib.py → ext/webhook.py} +45 -17
  53. pyxllib/ext/win32lib.py +40 -0
  54. pyxllib/ext/wjxlib.py +88 -0
  55. pyxllib/ext/wpsapi.py +124 -0
  56. pyxllib/ext/xlwork.py +9 -0
  57. pyxllib/ext/yuquelib.py +1105 -0
  58. pyxllib/file/__init__.py +17 -0
  59. pyxllib/file/docxlib.py +761 -0
  60. pyxllib/{util → file}/gitlib.py +40 -27
  61. pyxllib/file/libreoffice.py +165 -0
  62. pyxllib/file/movielib.py +148 -0
  63. pyxllib/file/newbie.py +10 -0
  64. pyxllib/file/onenotelib.py +1469 -0
  65. pyxllib/file/packlib/__init__.py +330 -0
  66. pyxllib/{util → file/packlib}/zipfile.py +598 -195
  67. pyxllib/file/pdflib.py +426 -0
  68. pyxllib/file/pupil.py +185 -0
  69. pyxllib/file/specialist/__init__.py +685 -0
  70. pyxllib/{basic/_5_dirlib.py → file/specialist/dirlib.py} +364 -93
  71. pyxllib/file/specialist/download.py +193 -0
  72. pyxllib/file/specialist/filelib.py +2829 -0
  73. pyxllib/file/xlsxlib.py +3131 -0
  74. pyxllib/file/xlsyncfile.py +341 -0
  75. pyxllib/prog/__init__.py +5 -0
  76. pyxllib/prog/cachetools.py +64 -0
  77. pyxllib/prog/deprecatedlib.py +233 -0
  78. pyxllib/prog/filelock.py +42 -0
  79. pyxllib/prog/ipyexec.py +253 -0
  80. pyxllib/prog/multiprogs.py +940 -0
  81. pyxllib/prog/newbie.py +451 -0
  82. pyxllib/prog/pupil.py +1197 -0
  83. pyxllib/{sitepackages.py → prog/sitepackages.py} +5 -3
  84. pyxllib/prog/specialist/__init__.py +391 -0
  85. pyxllib/prog/specialist/bc.py +203 -0
  86. pyxllib/prog/specialist/browser.py +497 -0
  87. pyxllib/prog/specialist/common.py +347 -0
  88. pyxllib/prog/specialist/datetime.py +199 -0
  89. pyxllib/prog/specialist/tictoc.py +240 -0
  90. pyxllib/prog/specialist/xllog.py +180 -0
  91. pyxllib/prog/xlosenv.py +108 -0
  92. pyxllib/stdlib/__init__.py +17 -0
  93. pyxllib/{util → stdlib}/tablepyxl/__init__.py +1 -3
  94. pyxllib/{util → stdlib}/tablepyxl/style.py +1 -1
  95. pyxllib/{util → stdlib}/tablepyxl/tablepyxl.py +2 -4
  96. pyxllib/text/__init__.py +8 -0
  97. pyxllib/text/ahocorasick.py +39 -0
  98. pyxllib/text/airscript.js +744 -0
  99. pyxllib/text/charclasslib.py +121 -0
  100. pyxllib/text/jiebalib.py +267 -0
  101. pyxllib/text/jinjalib.py +32 -0
  102. pyxllib/text/jsa_ai_prompt.md +271 -0
  103. pyxllib/text/jscode.py +922 -0
  104. pyxllib/text/latex/__init__.py +158 -0
  105. pyxllib/text/levenshtein.py +303 -0
  106. pyxllib/text/nestenv.py +1215 -0
  107. pyxllib/text/newbie.py +300 -0
  108. pyxllib/text/pupil/__init__.py +8 -0
  109. pyxllib/text/pupil/common.py +1121 -0
  110. pyxllib/text/pupil/xlalign.py +326 -0
  111. pyxllib/text/pycode.py +47 -0
  112. pyxllib/text/specialist/__init__.py +8 -0
  113. pyxllib/text/specialist/common.py +112 -0
  114. pyxllib/text/specialist/ptag.py +186 -0
  115. pyxllib/text/spellchecker.py +172 -0
  116. pyxllib/text/templates/echart_base.html +11 -0
  117. pyxllib/text/templates/highlight_code.html +17 -0
  118. pyxllib/text/templates/latex_editor.html +103 -0
  119. pyxllib/text/vbacode.py +17 -0
  120. pyxllib/text/xmllib.py +747 -0
  121. pyxllib/xl.py +39 -0
  122. pyxllib/xlcv.py +17 -0
  123. pyxllib-0.3.197.dist-info/METADATA +48 -0
  124. pyxllib-0.3.197.dist-info/RECORD +126 -0
  125. {pyxllib-0.0.43.dist-info → pyxllib-0.3.197.dist-info}/WHEEL +4 -5
  126. pyxllib/basic/_1_strlib.py +0 -945
  127. pyxllib/basic/_2_timelib.py +0 -488
  128. pyxllib/basic/_3_pathlib.py +0 -916
  129. pyxllib/basic/_4_loglib.py +0 -419
  130. pyxllib/basic/__init__.py +0 -54
  131. pyxllib/basic/arrow_.py +0 -250
  132. pyxllib/basic/chardet_.py +0 -66
  133. pyxllib/basic/dirlib.py +0 -529
  134. pyxllib/basic/dprint.py +0 -202
  135. pyxllib/basic/extension.py +0 -12
  136. pyxllib/basic/judge.py +0 -31
  137. pyxllib/basic/log.py +0 -204
  138. pyxllib/basic/pathlib_.py +0 -705
  139. pyxllib/basic/pytictoc.py +0 -102
  140. pyxllib/basic/qiniu_.py +0 -61
  141. pyxllib/basic/strlib.py +0 -761
  142. pyxllib/basic/timer.py +0 -132
  143. pyxllib/cv/cv.py +0 -834
  144. pyxllib/cv/cvlib/_1_geo.py +0 -543
  145. pyxllib/cv/cvlib/_2_cvprcs.py +0 -309
  146. pyxllib/cv/cvlib/_2_imgproc.py +0 -594
  147. pyxllib/cv/cvlib/_3_pilprcs.py +0 -80
  148. pyxllib/cv/cvlib/_4_cvimg.py +0 -211
  149. pyxllib/cv/cvlib/__init__.py +0 -10
  150. pyxllib/cv/debugtools.py +0 -82
  151. pyxllib/cv/fitz_.py +0 -300
  152. pyxllib/cv/installer.py +0 -42
  153. pyxllib/debug/_0_installer.py +0 -38
  154. pyxllib/debug/_1_typelib.py +0 -277
  155. pyxllib/debug/_2_chrome.py +0 -198
  156. pyxllib/debug/_3_showdir.py +0 -161
  157. pyxllib/debug/_4_bcompare.py +0 -140
  158. pyxllib/debug/__init__.py +0 -49
  159. pyxllib/debug/bcompare.py +0 -132
  160. pyxllib/debug/chrome.py +0 -198
  161. pyxllib/debug/installer.py +0 -38
  162. pyxllib/debug/showdir.py +0 -158
  163. pyxllib/debug/typelib.py +0 -278
  164. pyxllib/image/__init__.py +0 -12
  165. pyxllib/torch/__init__.py +0 -20
  166. pyxllib/torch/modellib.py +0 -37
  167. pyxllib/torch/trainlib.py +0 -344
  168. pyxllib/util/__init__.py +0 -20
  169. pyxllib/util/aip_.py +0 -141
  170. pyxllib/util/casiadb.py +0 -59
  171. pyxllib/util/excellib.py +0 -495
  172. pyxllib/util/filelib.py +0 -612
  173. pyxllib/util/jsondata.py +0 -27
  174. pyxllib/util/jsondata2.py +0 -92
  175. pyxllib/util/labelmelib.py +0 -139
  176. pyxllib/util/onepy/__init__.py +0 -29
  177. pyxllib/util/onepy/onepy.py +0 -574
  178. pyxllib/util/onepy/onmanager.py +0 -170
  179. pyxllib/util/pyautogui_.py +0 -219
  180. pyxllib/util/textlib.py +0 -1305
  181. pyxllib/util/unorder.py +0 -22
  182. pyxllib/util/xmllib.py +0 -639
  183. pyxllib-0.0.43.dist-info/METADATA +0 -39
  184. pyxllib-0.0.43.dist-info/RECORD +0 -80
  185. pyxllib-0.0.43.dist-info/top_level.txt +0 -1
  186. {pyxllib-0.0.43.dist-info → pyxllib-0.3.197.dist-info/licenses}/LICENSE +0 -0
pyxllib/basic/arrow_.py DELETED
@@ -1,250 +0,0 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- # @Author : 陈坤泽
4
- # @Email : 877362867@qq.com
5
- # @Data : 2020/05/31
6
-
7
-
8
- import datetime
9
- import os
10
- import re
11
-
12
- import arrow
13
-
14
-
15
- class Datetime(arrow.Arrow):
16
- r"""时间戳类
17
-
18
- 包含功能:
19
- 各种日期格式 -> 标准日期格式 -> 各种类型的输出格式
20
- 以及日期间的运算
21
-
22
- TODO 这个库的初始化接口写的太灵活了,需要一定的限制,否则会造成使用者的模糊
23
- """
24
-
25
- def __init__(self, *argv):
26
- r"""超高智能日期识别,能处理常见的各种输入类型来初始化一个标准时间戳值
27
-
28
- # 建议使用 Datetime.now()
29
- >> Datetime() # 没有参数的时候,初始化为当前时间
30
- <Datetime [2020-03-22T01:00:07.035481+00:00]>
31
-
32
- # 正常初始化方法
33
- >>> Datetime(2017, 2, 5, 8, 26, 58, 861782) # 标准的时间初始化方法
34
- <Datetime [2017-02-05T08:26:58.861782+08:00]>
35
- >>> Datetime(2017) # 可以省略月、日等值
36
- <Datetime [2017-01-01T00:00:00+08:00]>
37
-
38
- # 建议使用 Datetime.strptime
39
- >>> Datetime('2019年3月6日', '%Y年%m月%d日') # 指定格式
40
- <Datetime [2019-03-06T00:00:00+08:00]>
41
- >>> Datetime('w200301周日', 'w%y%m%d周日') # 周日必须写全,有缺失会报ValueError
42
- <Datetime [2020-03-01T00:00:00+08:00]>
43
-
44
- >>> Datetime(180213)
45
- <Datetime [2018-02-13T00:00:00+08:00]>
46
- >>> Datetime('180213')
47
- <Datetime [2018-02-13T00:00:00+08:00]>
48
-
49
- >>> Datetime('2015-06-15_22-19-01_HDR.jpg')
50
- <Datetime [2015-06-15T22:19:01+08:00]>
51
- >>> Datetime('IMG_20150615_2219011234_HDR.jpg')
52
- <Datetime [2015-06-15T22:19:01.001234+08:00]>
53
- >>> Datetime('_2015.6.15_22:19:02')
54
- <Datetime [2015-06-15T22:19:02+08:00]>
55
- """
56
- dt = None
57
- # 1 没有参数则默认当前运行时间
58
- if not argv:
59
- dt = datetime.datetime.now()
60
- if not dt and isinstance(argv[0], datetime.date):
61
- dt = datetime.datetime(argv[0].year, argv[0].month, argv[0].day)
62
- if not dt and isinstance(argv[0], (datetime.datetime, arrow.Arrow, Datetime)): dt = argv[0]
63
- if not dt and isinstance(argv[0], float): dt = datetime.datetime.fromtimestamp(argv[0])
64
- # 2 优先尝试用标准的datetime初始化方法
65
- if not dt:
66
- dt = Datetime._datetime(argv)
67
- # 3 如果上述解析不了,且argv恰好为两个参数,则判断为使用strptime初始化
68
- if not dt and len(argv) == 2:
69
- dt = datetime.datetime.strptime(str(argv[0]), argv[1])
70
- # 4 判断是否我个人特用的六位日期标记
71
- if not dt:
72
- dt = Datetime._six_digits_date(argv[0])
73
- # 5 如果仍然解析不了,开始使用一个智能推导算法
74
- if not dt:
75
- dt = Datetime._parse_time_string(argv[0])
76
- # 6 最后任何解析方案都失败,则报错
77
- if not dt:
78
- print(f'无法解析输入的时间标记 argv: {argv},现重置为当前时间点')
79
- dt = datetime.datetime.now()
80
-
81
- super(Datetime, self).__init__(dt.year, dt.month, dt.day,
82
- dt.hour, dt.minute, dt.second, dt.microsecond,
83
- 'local')
84
-
85
- @classmethod
86
- def strptime(cls, data_strnig, format):
87
- raise NotImplementedError
88
-
89
- @staticmethod
90
- def _datetime(argv):
91
- args, n = list(argv), len(argv)
92
- if n < 3: # 若没填写月、日,默认1月、1日
93
- args = args + [1] * (3 - n)
94
- try:
95
- return datetime.datetime(*args)
96
- except (ValueError, TypeError) as e:
97
- return None
98
-
99
- @staticmethod
100
- def _six_digits_date(s):
101
- """主要是我个人常用的日期标注格式
102
- """
103
- s, dt = str(s), None
104
- if re.match(r'\d{6}$', s):
105
- year = int(s[:2])
106
- year = 2000 + year if year < 50 else 1900 + year # 小于50默认是20xx年,否则认为是19xx年
107
- dt = Datetime._datetime([year, int(s[2:4]), int(s[4:])])
108
- return dt
109
-
110
- @staticmethod
111
- def _parse_time_string(s):
112
- r"""只对2000~2099年的时间点有效
113
- """
114
- data, break_flag = [], False
115
-
116
- def parse(pattern, left=None, right=None):
117
- """通用底层解析器"""
118
- nonlocal break_flag, s
119
- if break_flag: return
120
-
121
- m = re.search(pattern, s)
122
- if m:
123
- d = int(m.group())
124
- if left and d < left:
125
- break_flag = True
126
- elif right and d > right:
127
- break_flag = True
128
- else:
129
- data.append(d)
130
- s = s[m.end():]
131
- else:
132
- break_flag = True
133
-
134
- parse(r'20\d{2}')
135
- parse(r'\d\d?', 1, 12) # 有连续两个数组就获得两个,否则获得一个也行
136
- parse(r'\d\d?', 1, 31) # 这样其实不严谨,有的月份不到31天,不过反正_datetime会返回None代表处理失败的
137
- parse(r'\d\d?', 0, 23)
138
- parse(r'\d\d?', 0, 59)
139
- parse(r'\d\d?', 0, 59)
140
- parse(r'\d{1,6}') # microsecond
141
-
142
- return Datetime._datetime(data)
143
-
144
- def replace(self, **kwargs):
145
- """
146
- 'weekday',1~7分别代表周一到周日
147
- >>> Datetime(180826).replace(weekday=1).strftime('%y%m%d周%k')
148
- '180820周一'
149
- >>> Datetime(180826).replace(weekday=3).strftime('%y%m%d周%k')
150
- '180822周三'
151
- """
152
- a = self
153
- if 'weekday' in kwargs:
154
- if set(kwargs.keys()) & {'year', 'month', 'day'}:
155
- raise ValueError('weekday参数不能混合年月日的修改使用')
156
- a = a - a.isoweekday() + kwargs['weekday'] # 先减去当前星期几,再加上1~7的一个目标星期
157
- del kwargs['weekday']
158
- if kwargs:
159
- a = a.replace(**kwargs)
160
- return Datetime(a)
161
-
162
- def __add__(self, other):
163
- """加减数值时,单位按天处理"""
164
- if isinstance(other, (int, float)):
165
- other = datetime.timedelta(other)
166
- return Datetime(super(Datetime, self).__add__(other))
167
-
168
- def __sub__(self, other):
169
- if isinstance(other, (int, float)):
170
- other = datetime.timedelta(other)
171
- return Datetime(super(Datetime, self).__sub__(other))
172
-
173
- def shift(self, **kwargs):
174
- """shift 有点像游标卡尺,可以左右两边进行加减移位操作,加减的对象可以是年月日时分秒和星期。
175
- TODO 这文档什么鬼,输出还有Arrow,改成Datatime呀
176
- >>> a = Datetime(2018, 8, 24)
177
- >>> a.shift(months=-1)
178
- <Datetime [2018-07-24T00:00:00+08:00]>
179
- >>> a.shift(months=-1).format("YYYYMM")
180
- '201807'
181
- >>> a.shift(weeks=1)
182
- <Datetime [2018-08-31T00:00:00+08:00]>
183
- """
184
- return Datetime(super(Datetime, self).shift(**kwargs))
185
-
186
- def to(self, tz):
187
- """to 可以将一个本地时区转换成其它任意时区,例如:
188
- >>> a = Datetime(2020, 6, 1, 17)
189
- >>> a.to("utc")
190
- <Datetime [2020-06-01T09:00:00+08:00]>
191
- >>> a.to("utc").to("local")
192
- <Datetime [2020-06-01T09:00:00+08:00]>
193
- >>> a.to("America/New_York")
194
- <Datetime [2020-06-01T05:00:00+08:00]>
195
- """
196
- return Datetime(super(Datetime, self).to(tz))
197
-
198
- def humanize(self, other=None, locale="zh", only_distance=False, granularity="auto"):
199
- r""" humanize 方法是相对于当前时刻表示为“多久以前”的一种可读行字符串形式,
200
- 默认是英文格式,指定 locale 可显示相应的语言格式。
201
- >>> a = Datetime.now().shift(hours=-6)
202
- >>> a.humanize()
203
- '6小时前'
204
- >>> a.humanize(locale='en')
205
- '6 hours ago'
206
-
207
- 这是从 https://mp.weixin.qq.com/s/DqD_PmrspMeytloV_o54IA 摘录的Arrow的文档
208
- 并不是该Datetime类本身的文档,但意思差不多,可以参考
209
- """
210
- return super(Datetime, self).humanize(other, locale, only_distance, granularity)
211
-
212
- def strftime(self, fmt='%Y/%m/%d'):
213
- r"""做了简单格式拓展
214
- 官方支持的符号: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
215
-
216
- 我自己扩展的功能:
217
- %k,周几:一、二、三、四、五、六、日
218
-
219
- >>> dt = Datetime(2020, 2, 28, 9, 10, 52)
220
- >>> dt.strftime('%y%m%d')
221
- '200228'
222
- >>> dt.strftime('%Y/%m/%d')
223
- '2020/02/28'
224
- >>> dt.strftime('%y%m%d周%k') # 我自己最常用的格式
225
- '200228周五'
226
- """
227
- # 先用占位符替代中文,和我扩展的%k等标记
228
- fmt1 = re.sub(r'([\u4e00-\u9fa5,。;?()【】、①-⑨]|%k)', r'{placeholder}', fmt)
229
- tag = super(Datetime, self).strftime(fmt1)
230
- if fmt1 != fmt:
231
- texts = re.findall(r'([\u4e00-\u9fa5,。;?()【】、①-⑨]|%k)', fmt)
232
- for i in range(len(texts)):
233
- tag = tag.replace('{placeholder}', texts[i], 1)
234
- tag = tag.replace('%k', '日一二三四五六'[self.isoweekday() % 7])
235
- return tag
236
-
237
-
238
- def demo_datetime():
239
- from .dprint import dprint
240
-
241
- dprint(Datetime.now())
242
- # [05]arrow_.py/247: Datetime.now()<__main__.Datetime>=<Datetime [2020-06-01T16:54:45.365788+08:00]>
243
-
244
- # 获取文件的创建时间,st_ctime获得的事timestamp格式 1579529472.2958975
245
- dprint(Datetime(os.stat(__file__).st_ctime))
246
- # [05]arrow_.py/251: Datetime.now()<__main__.Datetime>=<Datetime [2020-06-01T16:54:45.365788+08:00]>
247
-
248
-
249
- if __name__ == '__main__':
250
- demo_datetime()
pyxllib/basic/chardet_.py DELETED
@@ -1,66 +0,0 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- # @Author : 陈坤泽
4
- # @Email : 877362867@qq.com
5
- # @Data : 2020/05/30 21:32
6
-
7
-
8
- import chardet
9
-
10
- from .dprint import dprint
11
- from .judge import is_file
12
-
13
-
14
- def get_encoding(bstr, maxn=1024):
15
- """ 输入二进制字符串或文本文件,返回字符编码
16
-
17
- https://www.yuque.com/xlpr/pyxllib/get_encoding
18
-
19
- :param bstr: 二进制字符串、文本文件
20
- :param maxn: 分析字节数上限,越小速度越快,但也会降低精准度
21
- :return: utf8, utf-8-sig, gbk, utf16
22
- """
23
- # 1 读取编码
24
- if isinstance(bstr, bytes): # 如果输入是一个二进制字符串流则直接识别
25
- encoding = chardet.detect(bstr[:maxn])['encoding'] # 截断一下,不然太长了,太影响速度
26
- elif is_file(bstr): # 如果是文件,则按二进制打开
27
- # 如果输入是一个文件名则进行读取
28
- if bstr.endswith('.pdf'):
29
- dprint(bstr) # 二进制文件,不应该进行编码分析,暂且默认返回utf8
30
- return 'utf8'
31
- with open(bstr, 'rb') as f: # 以二进制读取文件,注意二进制没有\r\n的值
32
- bstr = f.read()
33
- encoding = chardet.detect(bstr[:maxn])['encoding']
34
- else: # 其他类型不支持
35
- return 'utf8'
36
- # 检测结果存储在encoding
37
-
38
- # 2 智能适应优化,最终应该只能是gbk、utf8两种结果中的一种
39
- if encoding in ('ascii', 'utf-8', 'ISO-8859-1'):
40
- # 对ascii类编码,理解成是utf-8编码;ISO-8859-1跟ASCII差不多
41
- encoding = 'utf8'
42
- elif encoding in ('GBK', 'GB2312'):
43
- encoding = 'gbk'
44
- elif encoding == 'UTF-16':
45
- encoding = 'utf16'
46
- elif encoding == 'UTF-8-SIG':
47
- # 保留原值的一些正常识别结果
48
- encoding = 'utf-8-sig'
49
- elif bstr.strip(): # 如果不在预期结果内,且bstr非空,则用常见的几种编码尝试
50
- # dprint(encoding)
51
- type_ = ('utf8', 'gbk', 'utf16')
52
-
53
- def try_encoding(bstr, encoding):
54
- try:
55
- bstr.decode(encoding)
56
- return encoding
57
- except UnicodeDecodeError:
58
- return False
59
-
60
- for t in type_:
61
- encoding = try_encoding(bstr, t)
62
- if encoding: break
63
- else:
64
- encoding = 'utf8'
65
-
66
- return encoding