python如何查看编码
python中可以使用chardet模块检测字符串/文件编码。
1、chardet下载与安装
下载地址:http://pypi.python.org/pypi/chardet
下载chardet后,解压chardet压缩包,直接将chardet文件夹放在应用程序目录下,就可以使用importchardet开始使用chardet了,也可以将chardet拷贝到Python系统目录下,这样你所有的python程序只要用importchardet就可以了。
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()
以上内容为大家介绍了python如何查看编码,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:开发教育。
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!