chardet库:识别文件的编码格式
chardet库文档
http://chardet.readthedocs.io/en/latest/usage.html
小文件的编码判断
detect函数只需要一个 非unicode字符串参数,返回一个字典。该字典包括判断到的编码格式及判断的置信度。
with open('test1.txt', 'rb') as f:
result = chardet.detect(f.read())
print(result)
返回结果
{'encoding': 'utf-8', 'confidence': 0.99}
百分之99可能为utf-8编码。
测试构建函数,输入文件路径后识别任意小文件并输出:
import chardet
path1='/home/ifnd/下载/oracle1.asc'
path2='/home/ifnd/下载/oracle.asc'
def load_date(file_path):
str_cod=chardet.detect(open(file_path,'rb').read())['encoding']
with open(file_path,'r',encoding=str_cod) as f:
iter_f=''.join(iter(f)) return iter_f
print(load_date(path1))
大文件的编码判断
考虑到有的文件非常大,如果使用上述方法,全部读入后再判断编码格式,效率会变得非常低下。因此这里对读入的数据进行分块迭代,每次迭代出的数据喂给detector,当喂给detector数据达到一定程度足以进行高准确性判断时, detector.done返回 True。此时我们就可以获取该文件的编码格式。
from chardet.universaldetector import UniversalDetector
bigdata = open('test2.txt','rb')
detector = UniversalDetector()
for line in bigdata.readlines():
detector.feed(line)
if detector.done:
break
detector.close()
bigdata.close()
print(detector.result)
返回结果
{'encoding': 'utf-8', 'confidence': 0.99}
多个大文件的编码判断
如果想判断多个文件的编码,我们可以重复使用单个的UniversalDetector对象。只需要在每次调用UniversalDetector对象时候,初始化 detector.reset(),其余同上。
import os
from chardet.universaldetector import UniversalDetector
detector = UniversalDetector()
dirlist = os.dirlist('/Users/suosuo/Desktop/Test')
for name in dirlist:
"""
代码为mac上测试,如果为win
path = os.getcwd()+'\\%s'%name
"""
path = os.getcwd()+'/%s'%name
detector.reset()
for line in open(path, 'rb').readlines():
detector.feed(line)
if detector.done:
break
detector.close()
print(detector.result)
输出结果
{'encoding': 'utf-8', 'confidence': 0.99}
{'encoding': 'gb2312', 'confidence': 0.99}
......
{'encoding': 'utf-8', 'confidence': 0.99}
chardet库:识别文件的编码格式的更多相关文章
- linux查看文件的编码格式的方法 set fileencoding PYTHON
linux查看文件的编码格式的方法 set fileencoding 乱码原因:因为你的文件声明为utf-8,并且也应该是用utf-8的编码保存的源文件.但是windows的本地默认编码是cp93 ...
- C#猜测识别文件编码
项目 gitee地址:https://gitee.com/dhclly/IceDog.SmallProject/tree/master/src/IceDog.SmallProject.CodeConv ...
- python 根据文件的编码格式读取文件
因为各种文件的不同格式,导致导致文件打开失败,这时,我们可以先判断文件的编码吗格式,然后再根据文件的编码格式进行读取文件 举例:有一个data.txt文件,我们不知道它的编码格式,现在我们需要读取文件 ...
- iOS静态库.a文件制作和导入使用
iOS静态库.a文件制作: 1.新建Cocoa Touch Static Library工程 新建工程 - 选择iOS-FrameWork&Libary,选择 Cocoa Touch Stat ...
- 【转】IOS静态库a文件制作流程
原文网址:http://www.jianshu.com/p/3439598ea61f 1.新建Cocoa Touch Static Library工程 新建工程 2.Xcode的参数设置 " ...
- python之chardet库
chardet库是python的字符编码检测器,能够检测出各种编码的类型,例如: import chardet import urllib.request testdata = urllib.requ ...
- Linux下c函数dlopen实现加载动态库so文件代码举例
dlopen()是一个强大的库函数.该函数将打开一个新库,并把它装入内存.该函数主要用来加载库中的符号,这些符号在编译的时候是不知道的.这种机制使得在系统中添加或者删除一个模块时,都不需要重新编译了. ...
- SharePoint 文档库实现文件夹拖放到文档库
打开文档库-> 选择文件夹-> 在Ribbon中选择“库(list)”-> 在右边可以看到打开方式-> 选择用资源管理器打开-> 在新打开的资源管理器中可能实现对文夹的拖 ...
- 关于使用cocoaPods,import导入时第三方库头文件没有提示问题的解决办法
估计有很多朋友在使用Xcode在项目中导入第三方库时,使用的是cocoaPods,但是在项目中使用此第三方库导入头文件时,输入#import后,并不会联想第三方库头文件(XXXX.h),需要我们手动输 ...
随机推荐
- angularJS 状态样式绑定
angularJS提供输入框不同状态下的样式绑定 输入框有4种状态 ng-model 指令可以为应用数据提供状态值(invalid, dirty, touched, error): <!DOCT ...
- js 时间对比
https://www.cnblogs.com/xiangsj/p/7977325.html http://www.jb51.net/article/45560.htm isOverdue (time ...
- spring-cloud-config笔记
忽略元数据末尾 回到原数据开始处 spring-cloud-config 简单来讲就是spring-cloud实现的分布式配置中心.与之前介绍的开源配置服务方案 disconf是一样的,spring- ...
- MVC页面跳转,路径重复的问题
window.location.replace("../Home/xxx") 这是js路径跳转的示范,如果普通超链接也一样 前面加一个../
- 多线程-Thread与Runnable源码分析
Runnable: @FunctionalInterface public interface Runnable { /** * When an object implementing interfa ...
- flink on yarn 用户代码获取keytab本地文件和principal的方法
flink on yarn的情况下配置的keytab文件会根据每次yarn application 分配taskmanager的变化都是不一样的,在部分场景下用户代码也需要获得keytab文件在yar ...
- linux学习笔记26--命令wc
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的行数.字数.字节数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的行数.字 ...
- java 读写文件常用方法
package study.bigdata; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; ...
- mysql数据库表修改某一列的类型
下面列出:1.增加一个字段alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一个字段,默认为空alter table use ...
- maven项目编译:程序包com.sun.image.codec.jpeg不存在 这个类文件的位置在jre/lib/rt.jar
转载:http://superich2008.iteye.com/blog/2047830 失败提示信息为:程序包com.sun.image.codec.jpeg不存在 这个类文件的位置在jre/li ...