1

I tried the example in the following link: Write Python DataFrame as CSV into Azure Blob But BlockBlobService is no longer available in azure.storage.blob, so I tried using BlockBlobClient and I can create, delete containers using the following code. However I can't find a way to create a blob in the container and write the records from a data frame into it as mentioned in the above link. Please help I want to create a blob and write the records from the dataframe into it.

from azure.storage.blob import BlobServiceClient

blobService = BlobServiceClient.from_connection_string("**")
#blobService = BlobServiceClient(account_name=accountName, account_key=accountKey)
try:
   new_container = blobService.create_container("containerfromblobservice")
   properties = new_container.get_container_properties()
except ResourceExistsError:
   print("Container already exists.")
1
  • 1
    Do you have any other concerns? If you have no other concerns, could you please accept it as an answer? Commented Jun 17, 2020 at 0:56

1 Answer 1

6

Regarding the issue, please refer to the following code

# create data
head = ["col1" , "col2" , "col3"]
value = [[1 , 2 , 3],[4,5,6] , [8 , 7 , 9]]
df = pd.DataFrame (value, columns = head)
output = df.to_csv (index=False, encoding = "utf-8")
print(output)

connection_string=''
# Instantiate a new BlobServiceClient using a connection string
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
# Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client('mycsv')
try:
   # Create new Container in the service
   container_client.create_container()
   properties = container_client.get_container_properties()
except ResourceExistsError:
   print("Container already exists.")

# Instantiate a new BlobClient
blob_client = container_client.get_blob_client("output.csv")
# upload data
blob_client.upload_blob(output, blob_type="BlockBlob")

enter image description here

For more details, please refer to here and here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.