3

我想将存储在 Azure blob 存储中的 excel 文件读取到 python 数据框。我会使用什么方法?

4

1 回答 1

5

read_excel包中有一个函数name pandas,可以将在线excel文件的url传给函数,获取excel表的dataframe,如下图。

在此处输入图像描述

因此,您只需要使用 sas 令牌生成 excel blob 的 url,然后将其传递给函数。

这是我的示例代码。注意:它需要安装 Python 包azure-storage和.pandasxlrd

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

我使用我的示例 excel 文件来测试下面的代码,它工作正常。

testing.xlsx图 1.我test的 Azure Blob 存储容器中的示例 excel 文件

在此处输入图像描述

图 2. 我的示例 excel 文件的内容testing.xlsx

在此处输入图像描述

图 3. 我的示例 Python 代码读取 excel blob 的结果

在此处输入图像描述

于 2019-11-14T06:56:56.770 回答