我正在创建一个脚本,通过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')
请告诉我我哪里错了.