Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将多个gzip文件读取到一个fileobject_Python_File_Gzip_Python 2.x - Fatal编程技术网

在python中将多个gzip文件读取到一个fileobject

在python中将多个gzip文件读取到一个fileobject,python,file,gzip,python-2.x,Python,File,Gzip,Python 2.x,我想将多个gzip文件读取到一个文件对象 目前我正在做 import gzip a = gzip.open(path2zipfile1) for line in a.readline() #do some stuff 但是我需要读2个文件 a = gzip.open(path2zipfile1) #read zip1 a = gzip.open(path2zipfile2, 'rU') #appending file object with contents of 2nd file

我想将多个gzip文件读取到一个文件对象 目前我正在做

import gzip 
a = gzip.open(path2zipfile1) 
for line in a.readline()
  #do some stuff
但是我需要读2个文件

a = gzip.open(path2zipfile1)  #read zip1
a = gzip.open(path2zipfile2, 'rU') #appending file object with contents of 2nd file 
for line in a.readlines()
  #this should give me contents from zip1 then zip2
无法找到正确的模式执行此操作

请使用:

没有itertools的另一个版本:

def gen(files):
    for f in files:
        fo = gzip.open(f, 'rt')
        while True:
            line = fo.readline()
            if not line:
                break
            yield line

files = ['path2zipfile1', 'path2zipfile2']
for line in gen(files):
    print(line)
使用:

没有itertools的另一个版本:

def gen(files):
    for f in files:
        fo = gzip.open(f, 'rt')
        while True:
            line = fo.readline()
            if not line:
                break
            yield line

files = ['path2zipfile1', 'path2zipfile2']
for line in gen(files):
    print(line)

这对我很有用,你能告诉我做错了什么,以及itertools是如何让它工作的吗。TIA@pythonRcpp我解释得比我好。。。我想回答你关于文档的问题。@pythonRcpp我添加了一个没有
itertools
的版本,这基本上是等效的。我意识到我应该使用a.readlines()而不是a.readline()。edited@pythonRcpp我明白了,但答案没有受到影响。这对我来说很有效,你能告诉我我做错了什么,以及itertools是如何让它起作用的吗。TIA@pythonRcpp我解释得比我好。。。我想回答你关于文档的问题。@pythonRcpp我添加了一个没有
itertools
的版本,这基本上是等效的。我意识到我应该使用a.readlines()而不是a.readline()。edited@pythonRcpp我明白了,但答案没有受到影响。