您当前的位置: 首页 >  Python

Python编程:chardet模块检测byte字节编码

彭世瑜 发布时间:2018-06-14 17:21:11 ,浏览量:5

安装
pip install chardet
检测byte字节编码

confidence字段,表示检测的概率是1.0(即100%)


import chardet

ret = chardet.detect(b"hello world")
print(ret)
# {'encoding': 'ascii', 'confidence': 1.0}

data = '离离原上草,一岁一枯荣'.encode('gbk')
ret = chardet.detect(data)
print(ret)
# {'encoding': 'GB2312', 'confidence': 0.7407407407407407}

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

data = '最新の主要ニュース'.encode('euc-jp')
ret = chardet.detect(data)
print(ret)
# {'encoding': 'EUC-JP', 'confidence': 0.99}

参考

  1. 廖雪峰python - chardet
  2. chardet支持检测的编码列表
关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 5浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0461s