【发布时间】:2020-05-14 08:04:26
【问题描述】:
我有以下嵌套的字典,我试图在下面循环。好吧,不一定要循环,但我只想检查标签是否有值“EDD”。如果是这样,那么我想触发其他一些动作。
我的问题是我不断收到标签键错误的错误。
请问我该怎么做。
message.data = {
"messages": [{
"to": "wa-id",
"from": "another-wa-id",
"type": "text",
"_vnd": {
"v1": {
"direction": "outbound",
"in_reply_to": "an-earlier-inbound-external-id",
"author": {
"name": "the name of the author",
"type": "SYSTEM | OPERATOR",
},
"labels": [{
"uuid": "the-uuid",
"value": "EDD"
}]
}
}
}, ]
}
我的代码如下所示:
whatsapp_contact_id = message.data
print(whatsapp_contact_id.keys())
list_data = whatsapp_contact_id["messages"]
print(list_data)
for dictionary_data in list_data:
print(dictionary_data)
dictionary_keys = dictionary_data.items()
print(dictionary_keys)
"""
EDD_label = dictionary_data["labels"]
"""
EDD_label = dictionary_data.get('labels', 'could not find')
print("The label is below")
print(EDD_label)
【问题讨论】:
-
请提供完整的错误回溯
-
另外,请不要在代码中间为 cmets 使用三引号。它们适用于文档字符串,但除此之外使用 cmets。
标签: python arrays dictionary nested