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