读取gz文件内容|python如何按行获取gz包中的数据

读取gz文件内容|python如何按行获取gz包中的数据的第1张示图

A. python如何按行获取gz包中的数据

importosimportgzip#那是因为你调用了read方法,而这个方法会把文件一股脑儿读取出来的#为了便于你回迭代,你可以答在这里使用一个生成器defread_gz_file(path):ifos.path.exists(path):withgzip.open(path,'rt')aspf:forlineinpf:yieldlineelse:print('thepath[{}]isnotexist!'.format(path))con=read_gz_file('abc.gz')ifgetattr(con,'__iter__',None):forlineincon:print(line,end='')

B. 用java如何解析gz文件

一个偷懒的做法是调用操作系统命令把gz解压缩,然后再读取。网上也许能找到一些操作gz的java库。

C. Linux shell编程如何不解压读取gz压缩的文本

直接读取 一个gz压缩文件:我直接创建了一个内容:"asdfasdfasfd" 的 1.txt文件并用gzip 压缩。读取:$ zcat 1.txt.gz asdfasdfasfd如果内容过长可以接 less 查看:$ zcat 1.txt.gz | less

D. 如何让Hadoop读取以gz结尾的文本格式的文件

由于我的文件是gz结尾,所以hadoop把它当作了压缩文件,然后尝试解压缩后读取,所以解压失败了。于是去问google,没有搜到能够直接解决我问题的答案,但是搜到了此处相关的源代码:LineRecordReader.java;于是尝试着去阅读代码来解决问题,这个类很简单,继承自RecordReader,没有看到next函数和readLine函数,那就应该是基类实现的。很快发现了看名字是跟压缩解码相关的代码:private CompressionCodecFactory compressionCodecs = null;…compressionCodecs = new CompressionCodecFactory(job);final CompressionCodec codec = compressionCodecs.getCodec(file);…if (codec != null) {in = new LineReader(codec.createInputStream(fileIn), job);}else{…in = new LineReader(fileIn, job);}此处file就是拿到的文件路径,可以看到,应该就是通过CompressionCode.getCode(file)函数,拿到的codec类,然后读取的时候出异常了。那怎么让MapRece程序把这个.gz文件当作普通的文本文件呢?再点进去看CompressionCodeFactory.java的代码。getCodec函数的代码如下:/*** Find the relevant compression codec for the given file based on its* filename suffix.* @param file the filename to check* @return the codec object*/public CompressionCodec getCodec(Path file) {CompressionCodec result = null;if (codecs != null) {String filename = file.getName();String reversedFilename = new StringBuffer(filename).reverse().toString();SortedMap<String, CompressionCodec> subMap = codecs.headMap(reversedFilename);if (!subMap.isEmpty()) {String potentialSuffix = subMap.lastKey();if (reversedFilename.startsWith(potentialSuffix)) {result = codecs.get(potentialSuffix);}}}return result;}就是根据文件名称匹配来得到对应的解压缩类。咋们按图索骥,去看看codecs是在哪里赋值的:/*** Find the codecs specified in the config value io.compression.codecs * and register them. Defaults to gzip and zip.*/public CompressionCodecFactory(Configuration conf) {codecs = new TreeMap<String, CompressionCodec>();List<Class<? extends CompressionCodec>> codecClasses = getCodecClasses(conf);if (codecClasses == null) {addCodec(new GzipCodec());addCodec(new DefaultCodec());} else {Iterator<Class<? extends CompressionCodec>> itr = codecClasses.iterator();while (itr.hasNext()) {CompressionCodec codec = ReflectionUtils.newInstance(itr.next(), conf);addCodec(codec);}}}看样子从配置文件里,拿不到编码相关的配置,就会默认把GzipCodec,DefaultCodec加进去。再跟到getCodecClasses(conf)函数里去:/*** Get the list of codecs listed in the configuration* @param conf the configuration to look in* @return a list of the Configuration classes or null if the attribute* was not set*/public static List<Class<? extends CompressionCodec>> getCodecClasses(Configuration conf) {String codecsString = conf.get("io.compression.codecs");if (codecsString != null) {List<Class<? extends CompressionCodec>> result = new ArrayList<Class<? extends CompressionCodec>>();StringTokenizer codecSplit = new StringTokenizer(codecsString, ",");while (codecSplit.hasMoreElements()) {String codecSubstring = codecSplit.nextToken();if (codecSubstring.length() != 0) {try {Class<?> cls = conf.getClassByName(codecSubstring);if (!CompressionCodec.class.isAssignableFrom(cls)) {throw new IllegalArgumentException("Class " + codecSubstring +" is not a CompressionCodec");}result.add(cls.asSubclass(CompressionCodec.class));} catch (ClassNotFoundException ex) {throw new IllegalArgumentException("Compression codec " +codecSubstring + " not found.",ex);}}}return result;} else {return null;}}从这个函数里能够看到编码的配置是 io.compression.codecs 。可以看到,我们必须返回非null的result,那么直接让io.compression.codecs配置成空,应该就可以了,此时返回的result里面没有任何元素。问题解决方案:试了一把,执行这个MapRece程序时,加上 -Dio.compression.codecs=, 的参数,就可以了:hadoop jar ./mptools-0.1.jar dmp.tools.mr.Grep -Dio.compression.codecs=, "adgroupId=319356697" doc val

E. .gz文件怎么打开

这是一种压缩格式,很多在UNIX下使用的,你可以使用好压hapzip在windows下进行解压。

F. java 想不解压读取.gz文件内容,里面是xml格式的文本可以用dom4j再读取节点内容吗怎么实现

.gz是unix下的压缩格式。不是xml。

G. Qt怎么读取zip或者gz格式的文件

Excel.Application 是ActiveX 控件的名字QAxWidget::QAxWidget ( const QString & c, QWidget * parent = 0, Qt::WindowFlags f = 0 )创建一个QAxWidget ,并且初始化ActiveX 控件CQAxObject* workbooks = widget.querySubObject("WorkBooks");表示从widget中获取一个com对象这两行的意思应该是创建一个excel的QAxWidget,并且获取名为工作表的com对象

H. 求助如何用MATLAB直接读取.gz压缩文件

MATLAb直接读取貌似是不行的,我也研究过好长时间这个问题,也在网络知道上提问过,最后实在是没有办法,只好用MATLAB通过dos来调用WinRar进行解压。不过您要是给客户用的话,需要知道客户winRar的安装位置才行,给一下我的代码,希望能有些帮助。 pdir = ' F:\实验室\数据\avhrr201007\'; cd('D:\Program Files\WinRAR\'); dos(strcat('winrar e -ibck', pdir,file(i).name, pdir));pdir是数据存储的目录,F前面是有一个空格的。然后cd括号里面的是电脑中WinRar安装的位置,dos那一部分就是进行解压,要是不需要在后台解压,可以去掉 -iback

未经允许不得转载:山九号 » 读取gz文件内容|python如何按行获取gz包中的数据

赞 (0)