繁体   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