I have a dataframe that I want to upload to azure blob storage.
I'm using azure-storage-blob v12.3.2
from io import BytesIO, StringIO
from azure.storage.blob import BlobServiceClient
service_client = BlobServiceClient.from_connection_string('connection_string')
container_client = service_client.get_container_client('container_name')
output = StringIO()
df.to_csv(output)
container_client.upload_blob(name='output.csv', data=output)
This snippet doesn't work because upload_blob
expect bytes like BytesIO
but I can't pass BytesIO
to to_csv
because it expects a StringIO
.
How can I upload a buffered CSV directly into azure blob storage ?
---- EDIT ----
I found this solution :
df.to_csv(output)
output.seek(0)
bio = BytesIO(output.read().encode('utf8'))
container_client.upload_blob(name='output.csv', data=bio)
If there is a better way to do it, I take it.