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培训机构:千锋教育。

相关推荐HOT
更多>>
Python 枚举
1.枚举的定义首先,定义枚举要导入enum模块。枚举定义用class关键字,继承Enum类。用于定义枚举的class和定义类的class是有区别【下一篇博文继...详情>>
2023-11-06 13:46:00
Python多核编程mpi4py实践
一、概述CPU从三十多年前的8086,到十年前的奔腾,再到当下的多核i7。一开始,以单核cpu的主频为目标,架构的改良和集成电路工艺的进步使得cpu...详情>>
2023-11-06 09:21:36
python之增量式爬虫是什么?
引言:当我们在浏览相关网页的时候会发现,某些网站定时会在原有网页数据的基础上更新一批数据,例如某电影网站会实时更新一批最近热门的电影。...详情>>
2023-11-06 07:21:56
Python之父Guido谈Python的未来
Python在移动计算领域的应用Guido:移动对Python来说仍是一个很难啃下来的平台,但是并没有浏览器平台的难度大,因为Python实际上是可以运行在...详情>>
2023-11-06 02:14:20