教程集 www.jiaochengji.com
教程集 >  Python编程  >  python进阶  >  正文 python如何查看字符集

python如何查看字符集

发布时间:2021-01-08   编辑:jiaochengji.com
教程集为您提供python如何查看字符集等资源,欢迎您收藏本站,我们将为您提供最新的python如何查看字符集资源

python查看字符集的方法:可以利用第三方库chardet来进行判断。通过在命令行下执行【pip install chatdet】命令来安装chardet。使用方法如:【chardet.detect(b'Hello, world!')】。

Python利用第三方库chardet判断字符集。

(推荐教程:Python入门教程)

如果安装了Anaconda,chardet就已经可用了。否则,需要在命令行下通过pip安装:

$ pip install chardet

当我们拿到一个bytes时,就可以对其检测编码。用chardet检测编码,只需要一行代码:

>>> chardet.detect(b'Hello, world!')
{'encoding': 'ascii', 'confidence': 1.0, 'language': ''}

检测出的编码是ascii,注意到还有个confidence字段,表示检测的概率是1.0(即100%)。

对UTF-8编码进行检测:

>>> data = '离离原上草,一岁一枯荣'.encode('utf-8')
>>> chardet.detect(data)
{'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}

用chardet检测编码,使用简单。获取到编码后,再转换为str,就可以方便后续处理。

您可能感兴趣的文章:
python怎么检查元素是否在列表中存在
Python超级详细的变量命名规则
python如何追加字符串
python如何查看数据类型
r在python中表示什么意思
最全面的12种Python学习方式
python如何查看字符集
python中r代表什么意思
python怎么输出单词的字母
python字符串如何转化为列表

[关闭]
~ ~