我想将多个gzip文件读取到1个文件对象 目前我在做什么
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
无法找到合适的模式
答案 0 :(得分:2)
import itertools, gzip
files = ['path2zipfile1', 'path2zipfile2']
it = (gzip.open(f, 'rt') for f in files)
for line in itertools.chain.from_iterable(it):
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)