当前位置: 首页 > 图灵资讯 > 行业资讯> python chardet检测编码

python chardet检测编码

发布时间:2024-08-21 22:15:26

1、当我们得到一个bytes时,我们可以检测它的代码。用chardet检测代码只需要一行代码:

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

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

2、检测GBK编码的中文

>>>data='离开原草,一岁枯荣'.encode('gbk')
>>>chardet.detect(data)
{'encoding':'GB2312','confidence':0.7407407407407407,'language':'Chinese'}

检测编码为GB2312。请注意,GBK是GB2312的超集。它们是相同的编码。检测正确的概率为74%。language字段指出的语言为'Chinese'。

以上是python 希望chardet检测编码的方法对大家有所帮助。更多Python学习指导:python基础教程

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12