首页 > 解决方案 > 检查大于一个嵌套字典的键

问题描述

我有一个看起来像的文件:

{"phrase": "can you please talk with Mark and say school/workout/home/homework with slack", "entities": [{ "label": "name", "match": "Mark"}, { "label": "message"}]}
{"phrase": "can you please tweet must_not_put_on_heating_electric_costs_raised_by_\ufffd30pm_as_of_1/6 on whatsapp", "template": "can you please tweet <message> on whatsapp", "entities": [{ "label": "message", "match": "must_not_put_on_heating_electric_costs_raised_by_\ufffd30pm_as_of_1/6"}]}

我需要检查文件的每一行,看看“实体”键是否有多个嵌套字典。如果是这样,那么我需要访问该字典并添加一个匹配列,以便文件看起来像:

{"phrase": "can you please talk with Mark and say school/workout/home/homework with slack", "entities": [{ "label": "name", "match": "Mark"}, { "label": "message", "match": "school"}]}
{"phrase": "can you please tweet must_not_put_on_heating_electric_costs_raised_by_\ufffd30pm_as_of_1/6 on whatsapp", "template": "can you please tweet <message> on whatsapp", "entities": [{ "label": "message", "match": "must_not_put_on_heating_electric_costs_raised_by_\ufffd30pm_as_of_1/6"}]}

我真的不知道该怎么做?

到目前为止我的代码:

for row in data:
    entities_len = len(row['entities'])
    if entities_len >2 :
        try:
            row['target_entities'][1]['match'] = 'test' 
            print(row)

这没有按预期填充第二场比赛。

标签: pythonjsondictionary

解决方案


推荐阅读