提问人:LaggyFingers 提问时间:11/10/2023 最后编辑:Goku - stands with PalestineLaggyFingers 更新时间:11/11/2023 访问量:80
Azure Function App 错误:AttributeError:“str”对象在从字典中提取数据时没有属性“get”
Azure Function App Error: AttributeError: 'str' object has no attribute 'get' when extracting a data from a dictionary
问:
我开发的函数应用突然出现错误,我使用 .get() 方法从字典中提取数据。下面是我使用的示例代码:
def sendOpenAIPromptRequest(azureopenai_endpoint, azureopenai_deployment,azureopenai_token, azureopenai_version, workItemDetails):
workItemSummary = []
#Iteration per object in workItemDetails
for item in workItemDetails:
openai_prompt = openAIPrompt(item)
try:
url = f"{azureopenai_endpoint}/openai/deployments/{azureopenai_deployment}/chat/completions?{azureopenai_version}"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {azureopenai_token}"
}
body = {
"max_tokens": 590,
"messages": [
{
"role": "user",
"content": openai_prompt
}
],
"temperature": 0.25
}
response = requests.post(url=url, headers=headers, json=body)
print(response)
responseContent = json.loads(response.text)
except:
return print("An error has occurred.")
container = {
"UserStoryId": item.get("UserStoryId", ""),
"UserStoryTitle": item.get("UserStoryTitle", ""),
"UserStoryState":item.get("UserStoryState", ""),
"UserStoryType": item.get("UserStoryType", ""),
"Summary": responseContent['choices'][0]['message']['content']
}
workItemSummary.append(container)
return json.dumps({"workItemSummaryValue": workItemSummary})
workItemDetails 是一个字典列表,我需要在其中将其数据传输到“容器”变量。
workItemDetails = [
{
"UserStoryId": "id1",
"UserStoryTitle": "title1",
"UserStoryState": "state1",
"UserStoryType": "type1"
"RawUpdates": "updates1",
"RawComments": "comments1"
},
{
"UserStoryId": "id2",
"UserStoryTitle": "title2",
"UserStoryState": "state2",
"UserStoryType": "type2"
"RawUpdates": "updates2",
"RawComments": "comments2"
}]
在过去的几周里,它运行良好,但是当我从VScode或已部署的应用程序再次运行它时,它将返回此错误:
[2023-11-09T23:41:43.106Z] Executed 'Functions.MyFunctionApp' (Failed, Id=d91f7dc1-8c42-477d-a506-6128fd5e6898, Duration=10120ms) [2023-11-09T23:41:43.109Z] System.Private.CoreLib: Exception while executing function: Functions.MyFunctionApp. System.Private.CoreLib: Result: Failure Exception: AttributeError: 'str' object has no attribute 'get'
请帮我解决这个问题。
我需要将数据从字典传输到 Azure 函数应用中的另一个字典变量。
答:
0赞
Goku - stands with Palestine
11/10/2023
#1
您可以通过以下方式检查数据类型:
for item in workItemDetails:
print(type(item))
我假设你是这样的:workItemDetails
workItemDetails = ['{ "UserStoryId": "id1","UserStoryTitle": "title1","UserStoryState": "state1", "UserStoryType": "type1","RawUpdates": "updates1","RawComments": "comments1"}',
'{"UserStoryId": "id2","UserStoryTitle": "title2","UserStoryState": "state2","UserStoryType": "type2","RawUpdates": "updates2","RawComments": "comments2"}']
现在,如果您这样做:
for item in workItemDetails:
container = {
"UserStoryId": item.get("UserStoryId", ""),
"UserStoryTitle": item.get("UserStoryTitle", ""),
"UserStoryState":item.get("UserStoryState", ""),
"UserStoryType": item.get("UserStoryType", ""),
}
您将收到以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-12-a611593be7a6> in <cell line: 1>()
1 for item in workItemDetails:
2 container = {
----> 3 "UserStoryId": item.get("UserStoryId", ""),
4 "UserStoryTitle": item.get("UserStoryTitle", ""),
5 "UserStoryState":item.get("UserStoryState", ""),
AttributeError: 'str' object has no attribute 'get'
解决方法:
from ast import literal_eval
workItemDetails = [literal_eval(item) for item in workItemDetails]
for item in workItemDetails:
container = {
"UserStoryId": item.get("UserStoryId", ""),
"UserStoryTitle": item.get("UserStoryTitle", ""),
"UserStoryState":item.get("UserStoryState", ""),
"UserStoryType": item.get("UserStoryType", ""),
}
print(container)
{'UserStoryId': 'id2',
'UserStoryTitle': 'title2',
'UserStoryState': 'state2',
'UserStoryType': 'type2'}
评论
workItemDetails = [ { "UserStoryId": "id1", "UserStoryTitle": "title1", "UserStoryState": "state1", "UserStoryType": "type1" "RawUpdates": "updates1", "RawComments": "comments1" }, { "UserStoryId": "id2", "UserStoryTitle": "title2", "UserStoryState": "state2", "UserStoryType": "type2" "RawUpdates": "updates2", "RawComments": "comments2" }]
item