Python如何处理编码即chardet模块的使用方法
作者:redrose2100   类别:    日期:2022-05-10 09:44:45    阅读:1175 次   消耗积分:0 分

一、安装

安装命令

  1. pip install chardet

二、使用

2.1 检测是否为ascii编码

  1. import chardet
  2. v1=b"hello world"
  3. print(chardet.detect(v1))

执行结果为:

  1. {'encoding': 'ascii', 'confidence': 1.0, 'language': ''}

2.2 检测是否为gbk编码

  1. import chardet
  2. v1="呵呵".encode("gbk")
  3. print(chardet.detect(v1))

执行结果为:

  1. {'encoding': 'ISO-8859-1', 'confidence': 0.73, 'language': ''}

2.3 检测是否为utf8编码

  1. import chardet
  2. v1="Python学习".encode("utf8")
  3. print(chardet.detect(v1))

执行结果为:

  1. {'encoding': 'utf-8', 'confidence': 0.7525, 'language': ''}
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/144
个人成就
  • 博客总数: 613 
  • 阅读总量: 642312 
  • 2022年 : 371 篇 
  • 2023年 : 211 篇 
  • 2024年 : 31 篇 
  • 2025年 : 0 篇 
测试开发技术全栈公众号
DevOps技术交流微信群