Minura Punchihewa
Minura Punchihewa

Reputation: 2035

Writing pandas DataFrame to Azure Blob Storage from Azure Function

I am writing a simple Azure Function to read an input blob, create a pandas DataFrame from it and then write it to Blob Storage again as a CSV. I have the code given below to read the file and convert it into a DataFrame,

import logging import pandas as pd import io

import azure.functions as func

def main(inputBlob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {inputBlob.name}\n"
                 f"Blob Size: {inputBlob.length} bytes")

    df = pd.read_csv(io.BytesIO(inputBlob.read()), sep='#', encoding='unicode_escape', header=None, names=range(16))
    logging.info(df.head())

How can I write this DataFrame out to Blob Storage?

Upvotes: 0

Views: 2460

Answers (1)

SaiKarri-MT
SaiKarri-MT

Reputation: 1301

I have uploaded the file with below code, target is the container and target.csv is the blob which we want to write and store.

blob_service_client = BlobServiceClient.from_connection_string(CONN_STR)
# WRITE HEADER TO A OUT PUTFILE
output_file_dest = blob_service_client.get_blob_client(container="target", blob="target.csv")
#INITIALIZE OUTPUT
output_str = ""
#STORE COULMN HEADERS
data= list()
data.append(list(["column1", "column2", "column3", "column4"]))

# Adding data to a variable. Here you can pass the input blob. Also look for the parameters that sets your requirement in upload blob.
output_str += ('"' + '","'.join(data[0]) + '"\n')   
output_file_dest.upload_blob(output_str,overwrite=True)

From the above code you can ignore #STORE COULMN HEADERS and replace with input blob read data which you have done it using pandas.

Upvotes: 1

Related Questions