python如何查看编码

图片[1]-python如何查看编码-uusu优素-乐高,模型,3d打印,编程

python中可以使用chardet模块检测字符串/文件编码。

1、chardet下载与安装

下载地址:http://pypi.python.org/pypi/chardet

下载chardet后,解压chardet压缩包,直接将chardet文件夹放在应用程序目录下,就可以使用import chardet开始使用chardet了,也可以将chardet拷贝到Python系统目录下,这样你所有的python程序只要用import chardet就可以了。

pythonsetup.pyinstall

使用中,chardet.detect()返回字典,其中confidence是检测精确度,encoding是编码形式。

示例:

网页编码判断:

>>>importurllib
>>>rawdata=urllib.urlopen('http://www.google.cn/').read()
>>>importchardet
>>>chardet.detect(rawdata)
{'confidence':0.98999999999999999,'encoding':'GB2312'}

文件编码判断

importchardet
tt=open('c:\\111.txt','rb')
ff=tt.readline()
#这里试着换成read(5)也可以,但是换成readlines()后报错
enc=chardet.detect(ff)
printenc['encoding']
tt.close()
原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容