周海汉 /文
2010.3.14
python 内码检测模块chardet,是从firefox中移植的,判定正确率比较高。
下载地址:http://chardet.feedparser.org/
ubuntu下如果遇到ImportError: No module named chardet
可以通过如下的命令自动安装:
zhouhh@zhh64:~$ sudo apt-get install python-chardet
chardet.detect(buffer)会返回一个字典。
chardet.detect(rawdata)
{'confidence': 0.98999999999999999, 'encoding': 'GB2312'}
其中confidence是可信度,encoding是编码。
下面是用法示例。
执行:
python encdet.py
http://zh.wikipedia.org/
utf-8
http://www.mankan.or.jp/
SHIFT_JIS
http://g.cn/
GB2312
http://www.programmer-club.com.tw/
Big5