Azure函数-将Pandas数据框转换为Excel,写入outputBlob流 [英] Azure Function - Pandas dataframe to Excel, write to outputBlob stream

查看:79
本文介绍了Azure函数-将Pandas数据框转换为Excel,写入outputBlob流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am试图从Azure函数将DataFrame写入outputBlob.我在弄清楚要使用哪个io流时遇到麻烦.

Am trying to write a DataFrame to an outputBlob from an Azure Function. I'm having trouble figuring out which io stream to use.

我的功能如下:

    import io
    import xlrd
    import pandas as pd

    def main(myblob: func.InputStream, outputBlob: func.Out[func.InputStream]):
        logging.info(f"Python blob trigger function processed blob \n"
                     f"Name: {myblob.name}\n"
                     f"Blob Size: {myblob.length} bytes")
    
        input_file = xlrd.open_workbook(file_contents = myblob.read())
        df = pd.read_excel(input_file)
        if not df.empty:
            output = io.BytesIO()
            outputBlob.set(runway1.to_excel(output))

我们如何将DataFrame保存到Azure函数可识别的流中以将Excel写入存储容器中?

How do we save the DataFrame to a stream that is recognisable by the Azure Function to write the excel to a Storage Container?

推荐答案

如果要将Excel中的DataFrame保存为Excel,请参考以下示例

If you want to save DataFrame as excel to Azure blob storage, please refer to the following example

  1. SDK

azure-functions==1.3.0
numpy==1.19.0
pandas==1.0.5
python-dateutil==2.8.1
pytz==2020.1
six==1.15.0
xlrd==1.2.0
XlsxWriter==1.2.9

  1. 代码

import logging
import io
import xlrd
import pandas as pd
import xlsxwriter 
import azure.functions as func


async def main(myblob: func.InputStream,outputblob: func.Out[func.InputStream]):
    
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n")
    input_file = xlrd.open_workbook(file_contents = myblob.read())
    df = pd.read_excel(input_file)
    if not df.empty:
            xlb=io.BytesIO()
            writer = pd.ExcelWriter(xlb, engine= 'xlsxwriter')
            df.to_excel(writer,index=False)
            writer.save()
            xlb.seek(0) 
            outputblob.set(xlb)
            logging.info("OK")

这篇关于Azure函数-将Pandas数据框转换为Excel,写入outputBlob流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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