将 Python DataFrame 作为 CSV 写入 Azure Blob [英] Write Python DataFrame as CSV into Azure Blob
问题描述
关于从/向 Azure blob 存储读取和写入 Python 对象,我有两个问题.
谁能告诉我如何将 Python 数据帧作为 csv 文件直接写入 Azure Blob,而不将其存储在本地?
我尝试使用函数
create_blob_from_text
和create_blob_from_stream
但它们都不起作用.将数据帧转换为字符串并使用
create_blob_from_text
函数将文件写入 blob,但作为普通字符串而不是 csv.df_b = df.to_string()block_blob_service.create_blob_from_text('test', 'OutFilePy.csv', df_b)
如何将Azure blob存储中的json文件直接读入Python?
- 谁能告诉我如何将 Python 数据帧写成 csv 文件直接导入 Azure Blob 而不将其存储在本地?
您可以使用
<块引用>2.如何将Azure blob存储中的json文件直接读入Python?
示例代码:
from azure.storage.blob import (块Blob服务)accountName = ***";accountKey = "***";容器名称 = test1"blobName = "test3.json";blobService = BlockBlobService(account_name=accountName, account_key=accountKey)结果 = blobService.get_blob_to_text(containerName,blobName)打印(结果.内容)
输出结果:
希望对你有帮助.
I have got two questions on reading and writing Python objects from/to Azure blob storage.
Can someone tell me how to write Python dataframe as csv file directly into Azure Blob without storing it locally?
I tried using the functions
create_blob_from_text
andcreate_blob_from_stream
but none of them works.Converting dataframe to string and using
create_blob_from_text
function writes the file into the blob but as a plain string but not as csv.df_b = df.to_string() block_blob_service.create_blob_from_text('test', 'OutFilePy.csv', df_b)
How to directly read a json file in Azure blob storage directly into Python?
- Can someone tell me how to write Python dataframe as csv file directly into Azure Blob without storing it locally?
You could use pandas.DataFrame.to_csv method.
Sample code:
from azure.storage.blob import (
BlockBlobService
)
import pandas as pd
import io
output = io.StringIO()
head = ["col1" , "col2" , "col3"]
l = [[1 , 2 , 3],[4,5,6] , [8 , 7 , 9]]
df = pd.DataFrame (l , columns = head)
print(df)
output = df.to_csv (index_label="idx", encoding = "utf-8")
print(output)
accountName = "***"
accountKey = "***"
containerName = "test1"
blobName = "test3.json"
blobService = BlockBlobService(account_name=accountName, account_key=accountKey)
blobService.create_blob_from_text('test1', 'OutFilePy.csv', output)
Output result:
2.How to directly read a json file in Azure blob storage directly into Python?
Sample code:
from azure.storage.blob import (
BlockBlobService
)
accountName = "***"
accountKey = "***"
containerName = "test1"
blobName = "test3.json"
blobService = BlockBlobService(account_name=accountName, account_key=accountKey)
result = blobService.get_blob_to_text(containerName,blobName)
print(result.content)
Output result:
Hope it helps you.
这篇关于将 Python DataFrame 作为 CSV 写入 Azure Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!