Python检测文本字符编码的工具主要有2种
cChardet比chardet快。(cChardet上有这么一句话:This library is faster than chardet.)
cChardet目前有2个版本:cchardet 2.1.0 和 cchardet 1.1.3
pip install cchardet
import urllib
import cchardet
rawdata = urllib.urlopen('http://www.xiaohuawanpian.com').read()
cchardet.detect(rawdata)
{'confidence': 0.9900000095367432, 'encoding': u'UTF-8'}
chardet目前的最新版本是:chardet 3.0.4
pip install chardet
import urllib
import chardet
rawdata = urllib.urlopen('http://www.xiaohuawanpian.com').read()
chardet.detect(rawdata)
{'confidence': 0.99, 'language': '', 'encoding': 'utf-8'}