1. 安装chardet
在命令行中,进入Python27\Scripts目录,输入以下的命令:easy_install chardet
2. 操作
import chardet f = open(‘file‘,‘r‘) fencoding=chardet.detect(f.read()) print fencoding
fencoding输出格式 {‘confidence‘: 0.96630842899499614, ‘encoding‘: ‘GB2312‘} ,只能判断是否为某种编码的概率。比较准确的结果了。输入参数为str类型。
备注:
原文:http://www.cnblogs.com/zhuhaiying/p/6282780.html