簡體   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. 但是,我找不到在容器中創建 blob 並將數據幀中的記錄寫入其中的方法,如上面的鏈接中所述。 請幫助我想創建一個 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.")

關於這個問題,請參考以下代碼

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

在此處輸入圖像描述

有關更多詳細信息,請參閱此處此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM