使用 Chardet 自动检测文本编码


Author: yifei / Created: June 2, 2017, 2:23 p.m. / Modified: June 2, 2017, 2:29 p.m. / Edit

python 中的 chardet 库可以用来猜测文件的编码

usage

pip install cchardet
In [1]: import cchardet as chardet

In [2]: chinese_bytes = '中文'.encode('utf-8')

In [3]: chardet.detect(chinese_bytes)
Out[3]: {'confidence': 0.7524999976158142, 'encoding': 'UTF-8'}

有任何问题可以发邮件到 kongyifei (at) gmail.com 讨论