繁体   English   中英

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

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

我有一个压缩的数据文件(都在一个文件夹中,然后压缩)。 我想在不解压缩的情况下阅读每个文件。 我尝试了几种方法,但没有任何方法可以在 zip 文件中输入文件夹。 我应该如何做到这一点?

zip 文件中没有文件夹:

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

用一个文件夹:

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

namelist()以递归方式返回存档中所有项目的列表。

您可以通过调用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

希望有帮助。

我让亚历克的代码工作。 我做了一些小的编辑:(注意,这不适用于受密码保护的 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)

我让 RichS 的代码起作用了。 我做了一些小的编辑:

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'))

如您所见,编辑很小。 我已经切换到 Python 3,ZipFile 类有一个大写的 F,并且输出从 b 字符串转换为 unicode 字符串。 仅当您尝试解压缩文本文件时才进行解码。

PS 我一点也不反对 RichS。 我只是觉得这会很有趣。 既实用又温和。 PPS 您可以使用密码从存档中获取文件: ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False)ZipFile.read(name, pwd=None) 如果您使用.read则没有上下文管理器,因此您只需执行

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM