【问题标题】:Azure Function - Pandas dataframe to Excel, write to outputBlob streamAzure 函数 - Pandas 数据帧到 Excel,写入 outputBlob 流
【发布时间】:2020-07-09 10:14:43
【问题描述】:

我正在尝试将 DataFrame 从 Azure 函数写入 outputBlob。我无法确定要使用哪个 io 流。

我的函数如下所示:

    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 写入存储容器?

【问题讨论】:

  • 你能告诉我你的错误吗?

标签: python azure azure-functions azure-blob-storage


【解决方案1】:

如果要将DataFrame作为excel保存到Azure blob存储,请参考以下示例

  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")

【讨论】:

  • 谢谢吉姆。我的错误是没有将数据帧保存到 excel 写入器中,然后保存到字节流中。
  • 使用func.Out[func.InputStream]而不是func.Out[bytes]有什么意义?
猜你喜欢
  • 2021-09-06
  • 2020-11-17
  • 2020-07-12
  • 1970-01-01
  • 2022-01-23
  • 2021-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多