使用Jupyter笔记本使用python从存储在azure容器中的zip blob中提取特定文件

问题描述 投票:0回答:1

我在我的azure帐户中上传了zip文件,作为azure容器中的blob。 Zip文件包含.csv,.ascii文件和许多其他格式。我需要读取特定文件,让我们说ascii文件数据包含在zip文件中。我在这种情况下使用python。

如何从这个zip文件中读取特定文件数据而不在本地下载?我想只在内存中处理这个过程。

我也尝试使用azure为ML功能提供的jupyter笔记本我在这种情况下使用ZipFile python包。

请求您协助解决此问题

请查找以下代码段。

blob_service=BlockBlobService(account_name=ACCOUNT_NAME,account_key=ACCOUNT_KEY)
blob_list=blob_service.list_blobs(CONTAINER_NAME)

allBlobs = []
for blob in blob_list:
    allBlobs.append(blob.name)
sampleZipFile = allBlobs[0]
print(sampleZipFile) 
python azure zipfile msdn
1个回答
0
投票

您可以使用下面的代码来读取.zip文件内的文件,而无需在python中提取

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')

有关详细信息,请参阅ZipFile docs here

或者,你可以做这样的事情

-- coding: utf-8 --

“”创建于2019年4月1日星期一11:14:56

@author:moverm“”“

import zipfile

zfile = zipfile.ZipFile('C:\\LAB\Pyt\sample.zip')
for finfo in zfile.infolist():
    ifile = zfile.open(finfo)
    line_list = ifile.readlines()
    print(line_list)

这是相同的输出

enter image description here

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.