python通过chardet判断字符串编码格式

我用的python版本是2.7.13,安装后自带pip,所以安装chardet方便了很多,

注意:将pip的目录添加到系统环境变量中,否则无法识别pip命令

1.安装chardet
pip install chardet
2代码如下
print chardet.detect('rawdata')

返回字典类型

{'confidence': 0.98999999999999999, 'encoding': 'GB2312'}

其中confidence是可信度,encoding是编码。

chardet可以直接用detect函数来检测所给字符的编码。函数返回值为字典,有2个元数,一个是检测的可信度,另外一个就是检测到的编码。


原文出处:/show_1C7ew9GxKE2.html