您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Python使用chardet判断字符编码

2026/1/11 19:08:17发布19次查看
本文实例讲述了python使用chardet判断字符编码的方法。分享给大家供大家参考。具体分析如下:
python中chardet 用来实现字符串/文件编码检测模板
1、chardet下载与安装
下载地址:http://pypi.python.org/pypi/chardet
下载chardet后,解压chardet压缩包,直接将chardet文件夹放在应用程序目录下,就可以使用import chardet开始使用chardet了,也可以将chardet拷贝到python系统目录下,这样你所有的python程序只要用import chardet就可以了。
python setup.py install
2、实例
使用中,chardet.detect()返回字典,其中confidence是检测精确度,encoding是编码形式
(1)网页编码判断:
>>> import urllib>>> rawdata = urllib.urlopen('http://www.google.cn/').read()>>> import chardet>>> chardet.detect(rawdata){'confidence': 0.98999999999999999, 'encoding': 'gb2312'}
(2)文件编码判断
import chardettt=open('c:\\111.txt','rb')ff=tt.readline()#这里试着换成read(5)也可以,但是换成readlines()后报错enc=chardet.detect(ff)print enc['encoding']tt.close()
希望本文所述对大家的python程序设计有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product