简体   繁体   English

您如何(逐行)读取 Python 中压缩文件夹内的多个.gz 文件而不创建临时文件?

[英]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.

相关问题 如何在 Python 中读取压缩文件夹中的文本文件 - How to read text files in a zipped folder in Python Python - 如何读取以 xyz 开头的文件夹中的多个文件? - Python - How do you read multiple files in a folder starting by xyz? 如何在 Python 中读取压缩文件夹内文件夹中的文件 - How to read files in a folder within a zipped folder in Python 如何在 python 中以 dataframe 格式一次打开位于不同子文件夹中的多个 excel 压缩文件(.gz 文件)? - How to open multiple zipped excel files(.gz file) located in different sub folders at once in dataframe format in python? 使用python将目录内的压缩文件夹中的所有文件提取到其他目录而不使用文件夹 - Extract all files from a zipped folder inside a directory to other directory without folder using python 如何在python中的特定目录中读取多个.gz文件而不解压缩它们 - how to read multiple .gz files in a particular directory in python without unzipping them 读取 python 中一行的文件 - Read files of a line in python 读取.tar文件里面的.gz文件而不解压 - Read .gz files inside .tar files without extracting 如何读取.gz中的wav文件? - How do I read in wav files in .gz? 如何在python中逐行并行读取两个文件? - How to read two files in parallel, line by line, in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM