How to create CSV file in azure blob using python.

Shashank Shanu

8 months ago

CSV File In Azure
CSV File In Azure
Many times, you want your data to be saved in CSV format for future use. But working on some cloud platform such AWS, Azure and GCP you may don’t know how you can save your data into CSV file so that you may use it locally. Today in this article, I will try to explain to you how we can create a CSV file in azure blob using python. So, let’s start.
# Import Library
from import BlockBlobService
import pandas as pd
import tables
In the above code, we are importing the required libraries. In the first line of code, we are importing BlockBlobService from azure storage blob. And then we are import pandas which are used for data manipulation works and tables to create a table.
with BytesIO() as input_blob:
    with BytesIO() as output_blob:
        block_blob_service = BlockBlobService(account_name='STORAGEACCOUNTNAME', account_key='STORAGEACCOUNTKEY')
In the above code, we are creating BlockBlobService object which takes account_name and account_key as a parameter and then we are saving it to a variable name block_blob_service.

# Download as a stream
block_blob_service.get_blob_to_stream('STORAGEACCOUNTNAME', 'History.csv', input_blob)
With the above piece of code, we are downloading the previously created blob object as a stream with the azure .get_blob_to_stream() function. It takes storage account name, dataset and input blob.
# Do whatever you want to do - here I am just copying the input stream to the output stream
df=df.append(pd.DataFrame([["How to update password","","John"]],columns=['User Query','Email','User Name']))
#df_b = df.to_string()
In the above code, here we are just copying the input stream to the output stream with the help of pandas library.
First, we are reading the data using .read_csv() function of pandas library. In the next step, we are appending some data in the data frame. For saving the data frame into a CSV file we use .to_csv() function of python which saves data frame into a CSV file format.
#block_blob_service.create_blob_from_csv('STORAGEACCOUNTNAME', 'History.csv', df)
In the above code, with the help of .create_blob_from_path() which is used to creates a new blob from a file path, or updates the content of an existing blob, with automatic chunking and progress notifications.
I hope after you enjoyed reading this article and finally, you came to know about How to create CSV file in azure blob using python.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Programming…

Submit Review