简体   繁体   English

"如何在 Python 中将 Azure Blob 文件 CSV 转换为 Excel"

[英]How to convert Azure Blob file CSV to Excel in Python

I'm trying to convert the CSV file which is there in the azure storage container to EXCEL and place it in the same container.我正在尝试将 azure 存储容器中的 CSV 文件转换为 EXCEL 并将其放在同一个容器中。

I'm able to read the data from a CSV file with the below code and am not able to convert it into excel and upload it into the container.我可以使用以下代码从 CSV 文件中读取数据,但无法将其转换为 excel 并将其上传到容器中。

from io import StringIO
from azure.storage.blob import BlobServiceClient, ContainerClient, BlobClient
from typing import Container
import pandas as pd

conn_str = "DefaultEndpointsProtocol=https;AccountName="";AccountKey="";EndpointSuffix="""
container = "testing"
blob_name = "Test.csv"
filename = "test.xlsx"

container_client = ContainerClient.from_connection_string(
conn_str=conn_str, 
container_name=container
)   

downloaded_blob = container_client.download_blob(blob_name)

read_file = pd.read_csv(StringIO(downloaded_blob.content_as_text()) )

print(read_file)

Any suggestions on how can I achieve this?关于如何实现这一目标的任何建议?

You can convert the pandas Data Frame( read_file ) to excel file using to_excel API.您可以使用to_excel API 将to_excel Data Frame( read_file ) 转换为 excel 文件。 Since you want to upload it into the blob storage you can write it to the memory buffer first.由于要将其上传到 blob 存储中,因此可以先将其写入内存缓冲区。

import pandas as pd
from io import BytesIO

buffer = BytesIO()

# By setting the 'engine' in the ExcelWriter constructor.
writer = pd.ExcelWriter(buffer, engine="xlsxwriter")
# 'read_file' is your pandas DataFrame
read_file.to_excel(writer, sheet_name="Sheet1") 

# Save the workbook
writer.save()

# Seek to the beginning 
buffer.seek(0)

buffer now has the data that is ready to be uploaded to the blob storage. buffer现在具有准备上传到 blob 存储的数据。 So you can create a blob client instance first and use the upload_blob method to upload the excel file.所以可以先创建一个blob客户端实例,使用upload_blob方法上传excel文件。 Also set overwrite=True if you want to overwrite the file in Azure.如果要覆盖 Azure 中的文件,还要设置overwrite=True

excel_blob_client = BlobClient.from_connection_string(
conn_str=conn_str, 
container_name=container,
blob_name = "test.xlsx"
)  
excel_blob_client.upload_blob(buffer, overwrite=True)

References参考

Have your question got solved?你的问题解决了吗? I have same problem, if you found right python code , can you please share with me ?我有同样的问题,如果你找到了正确的python代码,你能和我分享一下吗?

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

相关问题 从 Azure blob 中读取 excel 数据并使用 Python azure 函数转换为 csv - Read excel data from Azure blob and convert into csv using Python azure function 使用 Python 从 azure blob 存储下载文件(csv、excel) - Download files (csv, excel) from azure blob storage using Python 如何从 Azure ADLS blob 容器中读取.xpt 格式文件并转换为 csv 格式 - How to read .xpt format file from the Azure ADLS blob container and convert to csv format 如何使用 Python 将 any.csv 文件转换为 excel 文件? - How to convert any .csv file to an excel file using Python? 如何使用 azure ZC1C425Z574E68385D1CAB1 编辑 azure blob 存储中的 *.csv 文件 - How to edit a *.csv file in the azure blob storage with an azure function? 使用python上载csv文件以使Blob存储蔚蓝 - Uploading csv file using python to azure blob storage 使用 python 创建 csv 文件并将其上传到 azure blob 存储 - Create and upload csv file to azure blob storage using python 使用 Python 3 将大型 CSV 文件转换为 Excel - Convert large CSV file to excel using Python 3 如何使用python csv writer写入azure输出blob? - How to write to azure output blob with python csv writer? 从 Azure Blob 存储中读取 CSV 文件,而不知道 python 中的 csv 文件名 - Read CSV file from Azure Blob Storage with out knowing the csv file name in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM