我正在创建一个脚本,通过VS代码扩展使用Azure函数在python中将csv转换为xlsx.下面提到的代码正在执行,没有任何问题.但是代码输出的文件没有打开,错误显示为

‘Excel无法打开该文件,因为文件格式或文件扩展名无效.请确认该文件未损坏,并且文件扩展名与该文件的格式匹配.’

import logging

import time

import pandas as pd

import azure.functions as func

from azure.storage.blob import BlobServiceClient

import os

 

def main(req: func.HttpRequest) -> func.HttpResponse:

logging.info('Python HTTP trigger function processed a request.')

filename = req.params.get("name")

# The following line is either a comment or an assignment, fix it as needed:

# filename = "input.csv"



STORAGEACCOUNTURL = 'URL'

STORAGEACCOUNTKEY = 'KEY'

LOCALFILENAME = '/tmp/' + str(filename)



CONTAINERNAME = 'demo'

BLOBNAME = str(filename)



# Download from blob

t1 = time.time()

blob_service_client_instance = BlobServiceClient(account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY)

blob_client_instance = blob_service_client_instance.get_blob_client(CONTAINERNAME, BLOBNAME)

with open(LOCALFILENAME, "wb") as my_blob:

    blob_data = blob_client_instance.download_blob()

    blob_data.readinto(my_blob)

t2 = time.time()



df = pd.read_csv(LOCALFILENAME)

targetFilename = filename.replace('.csv', '.xlsx')

print(targetFilename)

df.to_excel('/tmp/' + targetFilename, header=True)

ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))

print(ROOT_DIR + '/' + LOCALFILENAME)

container_client = blob_service_client_instance.get_container_client(container='demo')

with open(file=os.path.join(ROOT_DIR, LOCALFILENAME), mode="rb") as data:

    blob_client = container_client.upload_blob(name=targetFilename, data=data, overwrite=True)

return func.HttpResponse('success')

请告诉我我哪里错了.

推荐答案

100

我将一个CSV文件上传到存储帐户容器,如下所示:

enter image description here

Code:

import os
import logging
import azure.functions as func
import pandas as pd
from azure.storage.blob import BlobServiceClient

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    filename = req.params.get("name")
    if not filename:
        return func.HttpResponse("Error: 'name' parameter is missing or empty.", status_code=400)

    STORAGEACCOUNTURL = 'https://<storage_name>.blob.core.windows.net'
    STORAGEACCOUNTKEY = '<storage_key>'
    CONTAINERNAME = '<container_name>'
    BLOBNAME = str(filename)

    LOCALFILENAME = os.path.join(os.environ["TMP"], filename) 

    blob_service_client_instance = BlobServiceClient(account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY)
    blob_client_instance = blob_service_client_instance.get_blob_client(CONTAINERNAME, BLOBNAME)

    with open(LOCALFILENAME, "wb") as my_blob:
        blob_data = blob_client_instance.download_blob()
        blob_data.readinto(my_blob)

    df = pd.read_csv(LOCALFILENAME)
    targetFilename = filename.replace('<csvFileName>.csv', '<xlsxFileName>.xlsx')
    excel_file_path = os.path.join(os.environ["TMP"], targetFilename)
    df.to_excel(excel_file_path, header=True)

    container_client = blob_service_client_instance.get_container_client(container=CONTAINERNAME)
    with open(excel_file_path, mode="rb") as data:
        blob_client = container_client.upload_blob(name=targetFilename, data=data, overwrite=True)

    return func.HttpResponse(f'Success: Converted and uploaded {targetFilename}')

requirements.txt:

azure-functions
pandas
openpyxl
azure-storage-blob

Output:

enter image description here enter image description here

Output at Browser:

enter image description here

Azure Portal:

一百零二

enter image description here

xlsx Output data:

enter image description here

Python相关问答推荐

如何计算列表列行之间的公共元素

在Pandas 日历中插入一行

Select 用a和i标签包裹的复选框?

沿着数组中的轴计算真实条目

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

"使用odbc_connect(raw)连接字符串登录失败;可用于pyodbc"

启用/禁用shiny 的自动重新加载

将scipy. sparse矩阵直接保存为常规txt文件

matplotlib图中的复杂箭头形状

Python Pandas—时间序列—时间戳缺失时间精确在00:00

如何使用OpenGL使球体遵循Python中的八样路径?

python—telegraph—bot send_voice发送空文件

找到相对于列表索引的当前最大值列表""

巨 Python :逆向猜谜游戏

30个非DATETIME天内的累计金额

如何在GEKKO中使用复共轭物

freq = inject在pandas中做了什么?''它与freq = D有什么不同?''

python的文件. truncate()意外地没有截断'

极点替换值大于组内另一个极点数据帧的最大值

BeatuifulSoup从欧洲志愿者服务中获取数据和解析:一个从EU-Site收集机会的小铲子