简体   繁体   English

如何在 Python 中读取压缩文件夹中的文本文件

[英]How to read text files in a zipped folder in Python

I have a compressed data file (all in a folder, then zipped).我有一个压缩的数据文件(都在一个文件夹中,然后压缩)。 I want to read each file without unzipping.我想在不解压缩的情况下阅读每个文件。 I tried several methods but nothing works for entering the folder in the zip file.我尝试了几种方法,但没有任何方法可以在 zip 文件中输入文件夹。 How should I achieve that?我应该如何做到这一点?

Without folder in the zip file: zip 文件中没有文件夹:

with zipfile.ZipFile('data.zip') as z:
  for filename in z.namelist():
     data = filename.readlines()

With one folder:用一个文件夹:

with zipfile.ZipFile('data.zip') as z:
      for filename in z.namelist():
         if filename.endswith('/'):
             # Here is what I was stucked

namelist() returns a list of all items in an archive recursively. namelist()以递归方式返回存档中所有项目的列表。

You can check whether an item is a directory by calling os.path.isdir() :您可以通过调用os.path.isdir()来检查项目是否为目录:

import os
import zipfile

with zipfile.ZipFile('archive.zip') as z:
    for filename in z.namelist():
        if not os.path.isdir(filename):
            # read the file
            with z.open(filename) as f:
                for line in f:
                    print line

Hope that helps.希望有帮助。

I got Alec's code to work.我让亚历克的代码工作。 I made some minor edits: (note, this won't work with password-protected zipfiles)我做了一些小的编辑:(注意,这不适用于受密码保护的 zipfile)

import os
import sys
import zipfile

z = zipfile.ZipFile(sys.argv[1])  # Flexibility with regard to zipfile

for filename in z.namelist():
    if not os.path.isdir(filename):
        # read the file
        for line in z.open(filename):
            print line
        z.close()                # Close the file after opening it
del z                            # Cleanup (in case there's further work after this)

I got RichS' code to work.我让 RichS 的代码起作用了。 I made some minor edits:我做了一些小的编辑:

import os
import sys
import zipfile

archive = sys.argv[1] # assuming launched with `python my_script.py archive.zip`

with zipfile.ZipFile(archive) as z:    
    for filename in z.namelist():
        if not os.path.isdir(filename):
            # read the file
            for line in z.open(filename):
                print(line.decode('utf-8'))

As you can see the edits are minor.如您所见,编辑很小。 I've switched to Python 3, the ZipFile class has a capital F, and the output is converted from b-strings to unicode strings.我已经切换到 Python 3,ZipFile 类有一个大写的 F,并且输出从 b 字符串转换为 unicode 字符串。 Only decode if you are trying to unzip a text file.仅当您尝试解压缩文本文件时才进行解码。

PS I'm not dissing RichS at all. PS 我一点也不反对 RichS。 I just thought it would be hilarious.我只是觉得这会很有趣。 Both useful and a mild shitpost.既实用又温和。 PPS You can get file from an archive with a password: ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False) or ZipFile.read(name, pwd=None) . PPS 您可以使用密码从存档中获取文件: ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False)ZipFile.read(name, pwd=None) If you use .read then there's no context manager so you would simply do如果您使用.read则没有上下文管理器,因此您只需执行

            # read the file
            print(z.read(filename).decode('utf-8'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中读取压缩文件夹内文件夹中的文件 - How to read files in a folder within a zipped folder in Python 如何打开和读取文件夹python中的文本文件 - How to open and read text files in a folder python 您如何(逐行)读取 Python 中压缩文件夹内的多个.gz 文件而不创建临时文件? - How do you (line by line) read multiple .gz files that are inside a zipped folder in Python without creating temporary files? 遍历压缩在文件夹中的特定文件,并根据在 Python 中找到的文本/字符串移动它们 - iterate through specific files zipped in a folder and move them based on text/string found in Python 使用Python 2.7.5将文件夹中的所有压缩文件解压缩到同一文件夹 - Unzip all zipped files in a folder to that same folder using Python 2.7.5 用Python增量读取大型多部分压缩文本文件 - Incrementally Read Large Multipart Zipped Text File in Python 在python中逐行读取一个大的压缩文本文件 - Read a large zipped text file line by line in python 如何在python中读取文件夹中的txt文件列表 - how to read a list of txt files in a folder in python 如何在python中的文件夹中读取某些csv文件 - How to read some csv files in a folder in python 使用python读取box.com文件夹中的所有文本文件 - Read all the text files in a folder in box.com with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM