在python中将多个gzip文件读取到一个fileobject
我想将多个gzip文件读取到一个文件对象 目前我正在做在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
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我明白了,但答案没有受到影响。