使用python阅读Azure Blob [英] Read in azure blob using python
问题描述
我想将存储在Azure blob存储中的Excel文件读取到python数据框.我将使用哪种方法?
I want to read an excel file stored in Azure blob storage to a python data frame. What method would I use?
推荐答案
There is a function named read_excel
in the pandas
package, which you can pass a url of an online excel file to the function to get the dataframe of the excel table, as the figure below.
因此,您只需要生成一个带有sas令牌的excel blob的网址,然后将其传递给该函数即可.
So you just need to generate a url of a excel blob with sas token and then to pass it to the function.
这是我的示例代码.注意:它需要安装Python软件包azure-storage
,pandas
和xlrd
.
Here is my sample code. Note: it requires to install Python packages azure-storage
, pandas
and 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)
我使用我的示例excel文件来测试下面的代码,它工作正常.
I used my sample excel file to test the code below, it works fine.
图1.我的Azure Blob存储的test
容器中的示例excel文件testing.xlsx
Fig 1. My sample excel file testing.xlsx
in my test
container of Azure Blob Storage
图2.我的示例excel文件testing.xlsx
Fig 2. The content of my sample excel file testing.xlsx
图3.我的示例Python代码读取excel blob的结果
Fig 3. The result of my sample Python code to read excel blob
这篇关于使用python阅读Azure Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!