使用python zipfile提取zip子文件夹中的文件 [英] extract files inside zip sub folders with python zipfile

查看:101
本文介绍了使用python zipfile提取zip子文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文件和子 zip 文件夹的 zip 文件夹.我能够读取放置在父文件夹中的文件,但是如何访问子 zip 文件夹中的文件?这是我获取父文件夹中文件的代码

i have a zip folder that contains files and child zip folders. I am able to read the files placed in the parent folder but how can i get to the files inside the child zip folders? here is my code to get the files inside the parent folder

from io import BytesIO
import pandas as pd
import requests
import zipfile
url1 = 'https://www.someurl.com/abc.zip'
r = requests.get(url1)
z = zipfile.ZipFile(BytesIO(r.content))    
temp  = pd.read_csv(z.open('mno.csv')

我的问题是,如果可以说,我有一个子文件夹

my question is, if lets say, I have a child sub folder

xyz.zip 

包含文件

pqr.csv

如何读取这个文件

推荐答案

在尝试了一些排列组合后,我用这段代码解决了这个问题

After trying some permutation-combination, i hatched the problem with this code

zz = zipfile.ZipFile(z.namelist()[i])
temp2  = pd.read_csv(zz.open('pqr.csv'))
# where i is the index position of the child zip folder in the namelist() list. In this case, the 'xyz.zip' folder

# for eg if the 'xyz.zip' folder was third in the list, the command would be:
zz = zipfile.ZipFile(z.namelist()[2])

或者,如果索引位置未知,也可以这样实现:

alternatively, if the index position is not known, the same can be achieved like this:

zz  = zipfile.ZipFile(z.namelist()[z.namelist().index('xyz.zip')])

这篇关于使用python zipfile提取zip子文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆