[英]Reading a Zip File's Content from a Python Azure Function
I have a Python HttpTrigger Azure Function that I am posting a Zip file to.我有一个 Python HttpTrigger Azure 函数,我要将 Zip 文件发布到该函数。 The zip file contains a bunch of Excel files. zip 文件包含一堆 Excel 文件。 I want to read the content of the Excel files, in memory, and discard the zip file (no need to persist it).我想在内存中读取 Excel 文件的内容,然后丢弃 zip 文件(无需保留它)。
I tried the following:我尝试了以下方法:
<form enctype="multipart/form-data">
<input name="file" type="file" />
<input type="button" value="Upload" />
</form>
import logging
import azure.functions as func
import io
from zipfile import ZipFile
import pandas as pd
def extract_zip(content):
with ZipFile(io.BytesIO(content)) as thezip:
for zipinfo in thezip.infolist():
with thezip.open(zipinfo) as thefile:
yield zipinfo.filename, thefile
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
try:
file=req.files.get('file')
logging.info(file.filename)
# I am trying at first to read one file, then I am going to do a for-loop
for f_name, f_content in extract_zip(file):
# Process the name and the Excel files content
logging.info(file_name)
except Exception as ex:
logging.info(ex.args)
return func.HttpResponse(f"the file {file.filename} upload successfully")
No matter what I try, I cannot read the content of the Excel, the name is coming as expected.无论我尝试什么,我都无法读取 Excel 的内容,名称按预期出现。 What am I missing?我错过了什么?
Below is a simply code to achieve that you need:以下是实现您需要的简单代码:
import logging
import azure.functions as func
import os
from zipfile import ZipFile
import pandas
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
out_file = open("test.zip", "wb") # open for [w]riting as [b]inary
out_file.write(req.get_body())
out_file.close()
archive = ZipFile('test.zip','r')
files = archive.namelist()
for file in files:
logging.info(file)
book1 = archive.open('Book1.xlsx')
df = pandas.read_excel(book1)
#print the column names
logging.info(df.columns)
#get the values for a given column
values = df['test1'].values
logging.info(values)
#get a data frame with selected columns
FORMAT = ['test1', 'test2', 'test3']
values_t = df[FORMAT].values
logging.info(values_t)
name = req.params.get('name')
book1.close()
archive.close()
os.remove("test.zip")
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)
And you need to install pandas and xlrd, your requirement.txt file should be like this:并且你需要安装pandas和xlrd,你的requirement.txt文件应该是这样的:
azure-functions
pandas
xlrd
This is the content of my .xlsx file:这是我的 .xlsx 文件的内容:
And this is the result:这是结果:
I can success get, please have a try on your side.我可以成功,请在你这边试试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.