我尝试在我的Azure Function中解析查询参数(URL)中的JSON,该函数将JSON转换为XLSX并将其存储在存储容器中,但代码中有一个错误,我无法解决。下面给出的是错误代码。
文件“/home/site/wwwroot/.python_packages/lib/site-packages/pandas/core/internals/construction.py“,第114行,in arrays_to_mgr index = _extract_index(arrays)^文件“/home/site/wwwroot/.python_packages/lib/site-packages/pandas/core/internals/construction.py”,第667行,in _extract_index raise ValueError(“If using all scalar values,you must pass an index”)
这是我目前使用的代码,请让我知道我在哪里出错了!
import logging
import os
import json
import pandas as pd
import azure.functions as func
from azure.storage.blob import BlobServiceClient
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
# Retrieve JSON data from query parameters
json_data = req.params.get("json_data")
if not json_data:
return func.HttpResponse("Error: 'json_data' parameter is missing.", status_code=400)
# Parse the JSON data
try:
json_data = json.loads(json_data)
except json.JSONDecodeError:
return func.HttpResponse("Error: Invalid JSON data.", status_code=400)
# Create a DataFrame from the JSON data
df = pd.DataFrame(json_data)
# Specify the output XLSX file path
xlsx_file_path = '/tmp/output.xlsx'
# Convert the DataFrame to an XLSX file
df.to_excel(xlsx_file_path, index=False)
# Azure Blob Storage settings
STORAGEACCOUNTURL = MY_URL
STORAGEACCOUNTKEY = MY_KEY
CONTAINERNAME = 'demo'
BLOBNAME = 'output.xlsx'
# Create a BlobServiceClient to work with Blob Storage
blob_service_client_instance = BlobServiceClient(account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY)
# Get a container client
container_client = blob_service_client_instance.get_container_client(CONTAINERNAME)
# Upload the XLSX file to the container
with open(xlsx_file_path, mode="rb") as data:
blob_client = container_client.upload_blob(name=BLOBNAME, data=data, overwrite=True)
return func.HttpResponse(f'Success: Converted and uploaded {BLOBNAME}')
字符串
1条答案
按热度按时间dsekswqp1#
我在你的代码中做了一些修改,pd.DataFrame()构造函数在从JSON数据创建DataFrame时需要一个索引。
代码要求json_data是查询参数中对象的有效JSON数组。数组中的每个对象将被视为结果Excel文件中的一行。
工作代码-
字符串
输出-
Output Excel