簡體   English   中英

使用 Python 從 Azure Blob 下載 XLSX 文件

[英]Download XLSX File from Azure Blob in Python

from azure.storage.blob import BlockBlobService
block_blob_service = BlockBlobService(account_name=AZURE_ACCOUNT_NAME, account_key=AZURE_ACCOUNT_KEY)
file = block_blob_service.get_blob_to_bytes(AZURE_CONTAINER, "CS_MDMM_Global.xlsx")
file.content // the issue is at this line it give me data in some encoded form, i want to decode the data and store in panada data frame.

我正在從 blob 獲取編碼數據,但我無法弄清楚如何將數據解碼為 PANDA DATAFRAME。

聽起來您想通過pandas讀取存儲在Azure Blob 存儲中的xlsx blob 文件的內容以獲取pandas 數據幀。

我的 Azure Blob 存儲中存儲了一個xlsx示例文件,其內容如下圖所示。

在此處輸入圖片說明

所以我會直接通過Azure Storage SDK for Python and pandas讀取,下面第一步是安裝這些包。

pip install pandas azure-storage xlrd

這是我的示例代碼。

# Generate a url of excel blob with sas token
from azure.storage.blob.baseblobservice import BaseBlobService
from azure.storage.blob import BlobPermissions
from datetime import datetime, timedelta

account_name = '<your storage account name>'
account_key = '<your storage key>'
container_name = '<your container name>'
blob_name = '<your excel blob>'

blob_service = BaseBlobService(
    account_name=account_name,
    account_key=account_key
)

sas_token = blob_service.generate_blob_shared_access_signature(container_name, blob_name, permission=BlobPermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1))
blob_url_with_sas = blob_service.make_blob_url(container_name, blob_name, sas_token=sas_token)

# pass the blob url with sas to function `read_excel`
import pandas as pd
df = pd.read_excel(blob_url_with_sas)
print(df)

結果是:

在此處輸入圖片說明

實際上,您的帖子問題與另一個 SO 線程Read in azure blob using python重復,有關更多詳細信息,請參閱我的回答。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM