Home > OS >  How to read the files of Azure file share as csv that is pandas dataframe
How to read the files of Azure file share as csv that is pandas dataframe

Time:11-17

I have few csv files in my Azure File share which I am accessing as text by following the code:

from azure.storage.file import FileService

storageAccount='...'
accountKey='...'

file_service = FileService(account_name=storageAccount, account_key=accountKey)

share_name = '...'
directory_name = '...'
file_name = 'Name.csv'
file = file_service.get_file_to_text(share_name, directory_name, file_name)
print(file.content)

The contents of the csv files are being displayed but I need to pass them as dataframe which I am not able to do. Can anyone please tell me how to read the file.content as pandas dataframe?

CodePudding user response:

After reproducing from my end, I could able to read a csv file into dataframe from the contents of the file following the below code.

generator = file_service.list_directories_and_files('fileshare/')
for file_or_dir in generator:
    print(file_or_dir.name)
    file=file_service.get_file_to_text('fileshare','',file_or_dir.name)
    df = pd.read_csv(StringIO(file.content), sep=',')
    print(df)

RESULTS:

enter image description here

  • Related