简体   繁体   English

从 Python Azure 函数读取 Zip 文件的内容

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

相关问题 如何下载 Azure Function (Python).zip 文件? - How to download Azure Function (Python) .zip file? AWS Lambda-Python-在S3中读取打包的zip函数中的csv文件 - AWS Lambda - Python - reading csv file in S3-uploaded packaged zip function Python,阅读zip文件评论 - Python, reading a zip file comment 时间触发的 Python 中的 Azure 函数从 url 获取 zip 文件,解压缩,然后将文件输出到 Azure 存储中的 blob 容器 - Azure Function in Python that is on a time trigger get zip file from a url, unzip, then output file to blob container in Azure storage 从 python 中的 Azure Blob 读取 joblib 文件 - Reading joblib file from Azure Blob in python 从文件中读取并将内容分配给 Python 中的变量 - Reading from a file and assigning content to a variable in Python 使用AWS lambda函数使用boto3 python将S3文件从zip转换为gzip - Use AWS lambda function to convert S3 file from zip to gzip using boto3 python 从 Python 中的 S3 存储桶中读取 xml 个文件 - 仅存储最后一个文件的内容 - Reading xml files from S3 bucket in Python - Only the content of the last file is getting stored Python Paramiko,从文件中读取行 - Python Paramiko, reading line(s) from file 在python中读取zip文件时出现内存错误 - Memory error reading a zip file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM