[英]How do you (line by line) read multiple .gz files that are inside a zipped folder in Python without creating temporary files?
Let's say you have a zipped folder which contains many.gz files (the actual contents are just compressed CSVs).假设您有一个包含许多.gz 文件的压缩文件夹(实际内容只是压缩的 CSV)。 How do I directly read them line-by-line without first extracting into a temporary folder or files?
如何在不先提取到临时文件夹或文件的情况下直接逐行读取它们? It doesn't matter how they're stored once loaded in Python, I imagine naturally it would just be lists of strings, one string per row, but that's not important.
一旦加载到 Python 中,它们的存储方式并不重要,我想自然它只是字符串列表,每行一个字符串,但这并不重要。
You can do something like this你可以做这样的事情
from zipfile import ZipFile
import gzip
with ZipFile("storage.zip") as zf:
files = zf.namelist()
for file in files:
with zf.open(file) as f:
with gzip.open(f, 'rt') as g:
for line in g.readlines():
print(line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.