简体   繁体   English

Using azure.storage.blob to write Python DataFrame as CSV into Azure Blob

[英]Using azure.storage.blob to write Python DataFrame as CSV into Azure Blob

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. 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.但是,我找不到在容器中创建 blob 并将数据帧中的记录写入其中的方法,如上面的链接中所述。 Please help I want to create a blob and write the records from the dataframe into it.请帮助我想创建一个 blob 并将 dataframe 中的记录写入其中。

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.")

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")

在此处输入图像描述

For more details, please refer to here and here有关更多详细信息,请参阅此处此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 python 安装 azure.storage.blob 库? - How to install azure.storage.blob library for python? ImportError:没有名为azure.storage.blob的模块 - ImportError: No module named azure.storage.blob 尝试使用 Python 库 azure-storage-blob 时未解决的导入“azure.storage.blob” - Unresolved import 'azure.storage.blob' when trying to use Python library azure-storage-blob 将 Python DataFrame 写成 CSV 到 Azure Blob - Write Python DataFrame as CSV into Azure Blob 无法从 Azure 函数中的 azure.storage.blob 导入 AppendBlobService - Unable to import AppendBlobService from azure.storage.blob in Azure function ImportError:无法从“azure.storage.blob”导入名称“BlockBlobService” - ImportError: cannot import name 'BlockBlobService' from 'azure.storage.blob' Docker:无法从“azure.storage.blob”导入名称“BlobServiceClient” - Docker: cannot import name 'BlobServiceClient' from 'azure.storage.blob 导入azure.storage.blob时出现xml.etree错误 - xml.etree error when importing azure.storage.blob 无法从 azure.storage.blob 导入名称 BlockBlobService - Cannot import name BlockBlobService from azure.storage.blob 从“azure.storage.blob”导入“BlobServiceClient”时出错 - error importing 'BlobServiceClient' from 'azure.storage.blob'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM