在python中读取zip文件时出现内存错误 [英] Memory error reading a zip file in python
问题描述
我有一个线程,我正在其中使用 zipfile.ZipFile().read()
读取zip文件,在那里我遇到了内存错误.
I have a thread in which I am reading a zip file with zipfile.ZipFile().read()
, where I am getting a memory error.
我知道 read()
将整个文件加载到内存中.解压缩后的文件大小超过100MB.我还尝试了 zipfile.ZipFile().open().readlines()
,但是这花费了太多时间.
I am aware that read()
loads the entire file into memory. The size of file after unzipping is more than 100MB. I also tried with zipfile.ZipFile().open().readlines()
, but it takes too much time.
有什么方法可以快速读取文件而不会出现内存错误?
Is there any way that I can read the file with speed without getting memory error?
推荐答案
Assuming you're trying to read a zipped text file, you can treat the file-like object returned by ZipFile.open()
as an iterator, and process it line-by-line...
from zipfile import ZipFile
zip = ZipFile('myzip.zip')
stream = zip.open('myfile.txt')
for line in stream:
do_something_with(line)
这篇关于在python中读取zip文件时出现内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!