简体   繁体   中英

Read CSV file from Azure Blob Storage with out knowing the csv file name in python

In azure Blob storage i have CSV files. I need to read those CSV files into dataframe. Csv file name vary every time. So i need to read csv from from azure blobstorage container folder. Folder name is constant but csv file name vary.

Here is how you can read csv files to dataframes

from azure.storage.blob import BlockBlobService
import pandas as pd
from io import StringIO

STORAGEACCOUNTNAME= "<YOUR_STORAGE_ACCOUNTNAME>"
STORAGEACCOUNTKEY= "<YOUR_STORAGE_ACCOUNT_KEY>"
CONTAINERNAME= "<YOUR_CONTAINER_NAME>"
BLOBNAME= "<BLOB_NAME>"

blob_service=BlockBlobService(account_name=STORAGEACCOUNTNAME,account_key=STORAGEACCOUNTKEY)

blobstring = blob_service.get_blob_to_text(CONTAINERNAME,BLOBNAME).content
df = pd.read_csv(StringIO(blobstring))
print(df)

RESULTS:

在此处输入图像描述

REFERENCES: Explore data in Azure Blob storage with the pandas Python package

To get this resolved. You may want to consider either having the CSV files with generic names so as to call them generically. But since you mentioned the CSV file name changes. I'd suggest saving only that CSV in the container then call it using the code below:

file_loc = "wasbs://<continer name>@<storage account name>.blob.core.windows.net/*.csv
df = pd.read_csv(file_loc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM