【问题标题】:Read excel data from Azure blob and convert into csv using Python azure function从 Azure blob 读取 excel 数据并使用 Python azure 函数转换为 csv
【发布时间】:2020-12-28 17:15:02
【问题描述】:

我想部署具有以下功能的 azure 功能

  1. 将 Excel 数据从 Azure Blob 读取到流对象中,而不是下载到 VM。
  2. 读入数据帧 我需要帮助才能将 excel 文件读入数据框。如何更新放置架download_file_path以读取excel数据。
    import pandas as pd 
    import os 
    import io
    from azure.storage.blob import BlobClient,BlobServiceClient,ContentSettings
        
    connectionstring="XXXXXXXXXXXXXXXX" 
    excelcontainer = "excelcontainer"        
    excelblobname="Resource.xlsx" 
    sheet ="Resource" 
            
    blob_service_client =BlobServiceClient.from_connection_string(connectionstring)
    download_file_path =os.path.join(excelcontainer)
    blob_client = blob_service_client.get_blob_client(container=excelcontainer, blob=excelblobname)
    with open(download_file_path, "rb") as f:
       data_bytes = f.read()
    df =pd.read_excel(data_bytes, sheet_name=sheet, encoding = "utf-16")

【问题讨论】:

  • 你能告诉我你尝试了什么吗?
  • 我编写了脚本以将 blob(Excel) 下载到本地并使用 pandas 将数据读取到 dataframe 中。从数据框中,数据被推送到 csv 文件中。此 csv 文件已上传到 blob。当部署到 Azure 功能时,它会失败。我没有下载,而是尝试从 blob 流式传输数据并将其推送到数据帧中。它在with open(download_file_path, "rb") 失败,它无法找到资源

标签: python azure io


【解决方案1】:

如果你想用 panda 从 Azure blob 读取 excel 文件,你有两个选择

  1. 为 blob 生成 SAS 令牌,然后使用带有 SAS 令牌的 blob URL 来访问它
from datetime import datetime, timedelta
import pandas as pd
from azure.storage.blob import BlobSasPermissions, generate_blob_sas
def main(req: func.HttpRequest) -> func.HttpResponse:
    account_name = 'andyprivate'
    account_key = 'h4pP1fe76*****A=='
    container_name = 'test'
    blob_name="sample.xlsx"
    sas=generate_blob_sas(
      account_name=account_name,
      container_name=container_name,
      blob_name=blob_name,
      account_key=account_key,
      permission=BlobSasPermissions(read=True),
      expiry=datetime.utcnow() + timedelta(hours=1)
    )

    blob_url = f'https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}?{sas}'
    df=pd.read_excel(blob_url)
    print(df)
    ......

  1. 下载 blob
from azure.storage.blob import  BlobServiceClient
def main(req: func.HttpRequest) -> func.HttpResponse:
    account_name = 'andyprivate'
    account_key = 'h4pP1f****='

    blob_service_client = BlobServiceClient(account_url=f'https://{account_name }.blob.core.windows.net/', credential=account_key)
    blob_client = blob_service_client.get_blob_client(container='test', blob='sample.xlsx')
    downloader =blob_client.download_blob()
    df=pd.read_excel(downloader.readall())
    print(df)
    ....

【讨论】:

猜你喜欢
  • 2020-10-21
  • 2020-03-10
  • 2020-02-23
  • 2016-10-12
  • 2021-11-27
  • 2021-12-18
  • 2021-12-26
  • 2021-12-26
  • 2019-12-11
相关资源
最近更新 更多