python2.7:文件编码检测chardet 详解
2016-03-25 10:27
387 查看
# coding=utf-8 import os,chardet print os.getcwd() # 打印当前工作目录 os.chdir("C:\Users\Administrator\Desktop\Pyhton2.7Tests") # 修改当前工作目录 f = open("chardet_test.txt", "r") result = chardet.detect(f.read()) # chardet.detect()方法返回一个字典,confidence是精确度,encoding是编码格式。 print result f.close() import urllib2 # 测试网页编码 d = urllib2.urlopen("http://www.baidu.com") print chardet.detect(d.read()) d.close() # GB2312是中国规定的汉字编码,也可以说是简体中文的字符集编码;GBK 是 GB2312的扩展 ,除了兼容GB2312外,它还能显示繁体中文,还有日文的假名 # chardet.detect检测到的编码是GB2312,但是实际上的应该是 <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> # 网页是GBK,所以此时的精确度是99%。 # 输出 # D:\Program Files (x86)\Notepad++ # {'confidence': 1.0, 'encoding': 'ascii'} # {'confidence': 0.99, 'encoding': 'utf-8'} # 请按任意键继续. . .
相关文章推荐
- python2.7:urllib 和urllib2之间的区别
- Python基础教程之正则表达式基本语法以及re模块
- python安装MySQLdb模块-(ubuntu,windows)
- python函数里面的装饰器和闭包的理解
- Python中的sorted函数以及operator.itemgetter函数
- python2.7:IOError: [Errno 2] No such file or directory:
- python 如何重载模块,用一个简单的例子来解释
- PHP + PYTHON 多任务多线程,后台运行,计划任务-实现方法
- Python信号处理模块signal
- python中字典(dict)常见用法
- python2.7:模块安装4种方法
- Python基础教程
- python写个图片爬虫
- python中元组(tuple)用法总结
- python
- 转载一篇python类的博文
- Python入门 之 dict
- Python-pip 安装失败问题解决
- 初始Python类
- Python基础教程之正则表达式基本语法以及re模块