0
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.

I'm getting encoded data from blob but I'm unable to figure out how I will decode the data to PANDA DATAFRAME.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
Mueez Ahmad
  • 33
  • 1
  • 5

1 Answers1

0

It sounds like you want to read the content of a xlsx blob file stored in Azure Blob Storage via pandas to get a pandas dataframe.

I have a xlsx sample file stored in my Azure Blob Storage, its content is as the figure below.

enter image description here

So I will directly read it by Azure Storage SDK for Python and pandas, the first step is to install these packages below.

pip install pandas azure-storage xlrd

Here is my sample code.

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

And the result is:

enter image description here

Actually, your post question is duplicated with the other SO thread Read in azure blob using python, for more details, please refer to my answer for it.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43