Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将数据帧作为xlsx文件写入azure blob存储而不创建本地文件_Python_Excel_Azure - Fatal编程技术网

Python 将数据帧作为xlsx文件写入azure blob存储而不创建本地文件

Python 将数据帧作为xlsx文件写入azure blob存储而不创建本地文件,python,excel,azure,Python,Excel,Azure,我有以下使用本地文件的解决方案,但我想跳过它。有可能吗 blob_service_client = BlobServiceClient(account_url = url, credential = token) write_blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name) filename = "example.xlsx" df.to_exc

我有以下使用本地文件的解决方案,但我想跳过它。有可能吗

blob_service_client = BlobServiceClient(account_url = url, credential = token)
write_blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
filename = "example.xlsx"
df.to_excel(filename ,index = False)
with open(filename, "rb") as df:
write_blob_client.upload_blob(df, overwrite = True)
我没有试过最后三排

teststream = BytesIO(df.to_records(index = False).tostring())
write_blob_client.upload_blob(teststream, overwrite = True)

这会将excel文件写入blob存储,但当尝试打开它时,会出现一个错误,即文件扩展名与文件格式不匹配。

当然,您不需要创建本地文件

您只需要将一个流放入upload_blob。看起来您正在使用offcial提供的示例代码。您不需要将
open(文件名,“rb”)用作df
,只需将其转换为流即可

这是我的代码:

import logging

import azure.functions as func
import os, uuid, io
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    connect_str = os.getenv('str')
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)
    container_name = "video"
    container_client = blob_service_client.get_container_client(container_name)
    filename = "test.xlsx"
    blob_client = blob_service_client.get_blob_client(container_name, filename)
    teststream = io.BytesIO(req.get_body())
    blob_client.upload_blob(teststream, overwrite = True)
    name = req.params.get('name')

    return func.HttpResponse("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
上面的代码将请求主体从字节转换为流,并上载到azure blob存储


请告诉我您是否能解决此问题,如有疑问,请告诉我。

您必须从BytesIO对象获取值。下面的代码适合我

writer=io.BytesIO()
df.to_excel(编写器)
blob\u client.upload\u blob(writer.getvalue(),overwrite=True)

只需将数据转换为strean即可,无需创建本地文件。请看一下我的答案,让我知道你是否能解决这个问题。:)嗨,这个问题有什么进展吗?嗨,谢谢。仍在试图找出如何将数据流转换为数据流。你对此有什么建议吗?我编辑了我的问题以显示我的努力。嗨,你能详细说明一下吗?我正在尝试指定一个带时间戳的文件名…您好@KMoe,也许这个库可以帮助您: