将多个 gzip 文件读取到 python 中的 1 个文件对象
Read multiple gzip files to 1 fileobject in python
我想将多个 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
无法找到合适的模式
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)
我想将多个 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
无法找到合适的模式
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)