Python基础 检测多种语言编码 chardet

来源:互联网 发布:yum install chrome 编辑:程序博客网 时间:2024/05/21 17:12

检测语言

运行示例

#!/usr/bin/env python3# -*- coding: utf-8 -*-# Python基础 检测编码 chardetimport chardetresult = chardet.detect(b"Hello python")# {'encoding': 'ascii', 'confidence': 1.0, 'language': ''}# 1.0 表示正确率 100%print(result)chineseData = "万万没想到,我是王大锤".encode("gbk")result = chardet.detect(chineseData)# {'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}# 0.99 表示正确率 99%print(result)

运行结果

D:\PythonProject\sustudy>python main.py{'encoding': 'ascii', 'confidence': 1.0, 'language': ''}{'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}

chardet支持检测中文、日文、韩文等多种语言