dra*_*ams 5 python azure-blob-storage azure-functions azure-blob-trigger
我想将存储在 Azure blob 存储中的 Excel 文件读取到 python 数据框。我会使用什么方法?
read_excel包中有一个函数pandas,你可以将一个在线excel文件的url传递给该函数来获取excel表格的dataframe,如下图。
因此,您只需使用 sas 令牌生成 excel blob 的 url,然后将其传递给函数即可。
这是我的示例代码。注意:需要安装Python包azure-storage,pandas和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)
Run Code Online (Sandbox Code Playgroud)
我使用我的示例 Excel 文件来测试下面的代码,它工作正常。
图 1. Azure Blob 存储容器testing.xlsx中的示例 excel 文件test
图 2. 我的示例 Excel 文件的内容testing.xlsx
图 3. 我的示例 Python 代码读取 excel blob 的结果