Python专题系列(7)chardet专题(检测字符串编码格式)

Python专题系列(1)pathlib专题

Python专题系列(2)csv专题

Python专题系列(3)logging专题

Python专题系列(4)configparser专题

Python专题系列(5)setuptools专题

Python专题系列(6)pluggy专题

Python专题系列(7)chardet专题(检测字符串编码格式)

1、安装

1.1、安装命令
pip install chardet

2、使用

2.1、检测是否为ascii编码
import chardet

v1=b"hello world"
print(chardet.detect(v1))

执行结果为:

{
    
    'encoding': 'ascii', 'confidence': 1.0, 'language': ''}
2.2、检测是否为gbk编码
import chardet

v1="呵呵".encode("gbk")
print(chardet.detect(v1))

执行结果为:

{
    
    'encoding': 'ISO-8859-1', 'confidence': 0.73, 'language': ''}
2.3、检测是否为utf8编码
import chardet

v1="Python学习".encode("utf8")
print(chardet.detect(v1))

执行结果为:

{
    
    'encoding': 'utf-8', 'confidence': 0.7525, 'language': ''}

Guess you like

Origin blog.csdn.net/redrose2100/article/details/121321072