比较JSON文件中的多个词典?

时间:2019-01-17 12:53:17

标签: python json

现在我有一个看起来像这样的JSON文件:

"suggested":[{
    "city": "Berlin",
    "location": "Europe",
    "hotels": true,
    "restaurants": true,
    "rivers": false},
    {
        "city": "Andorra",
        "location": "Global",
        "hotels": false,
        "restaurants": true,
        "rivers": true
    }]

我想将在“字典”中拆分的JSON文件与此“参考” JSON文件进行比较:

"master":[{
    "city": "",
    "location": "Europe",
    "hotels": true,
    "restaurants": false,
    "rivers": false
}]

我的目标是将"master" JSON中的键的值与"suggested" JSON文件中的内部字典的值进行比较,并返回打印出它们具有多少个匹配项。例如:

"Berlin" matches with "Master" in (3) fields.

现在,我使用简单的功能在python内部打开了JSON文件:

def openJsonFile(file):
    with open (file) as json_data:
        workData = json.load(json_data)
        return(workData)

此功能可循环浏览打开的文件:

def compareJsonFiles(suggested, master):
    matches = 0
    for key in suggested.keys():
        value = suggested[key]
        if key not in master:
            print ("{0} doesn't have value in {1}".format(key, master))
        else:
            if master[key] == value:
                print("for key %s values match" % key)
                matches + 1
    print(matches)

但是当我尝试通过字典时,出现此错误:

AttributeError: 'list' object has no attribute 'keys'

更新: 我做了这个功能,试图将json列表中的每个值作为单独的字典传递。

def jsonToDict(file):
for lsdt in file:
    newListDic = file[lsdt]
    for key in newListDic.keys():
        value = newListDic[key]
        print(value)

它可以给我一个命令。但提示:

TypeError: list indices must be integers or slices, not dict

如何在JSON文件中循环浏览几个字典,然后将键值与另一个JSON文件进行比较?

P.S .:我已经搜索了近两天,但都没有成功,因此,如果帖子中发生的第一件事不是“标记为重复标记”,我将感到高兴。

3 个答案:

答案 0 :(得分:0)

您的master节点是列表,而不是字典。如果您确定母版始终只保留一个条目,则通过

compareJsonFiles(suggested_data, master_data[0])

答案 1 :(得分:0)

看看Cerberus(不要与Kerberos混淆) http://docs.python-cerberus.org/en/stable/usage.html#basic-usage

所需的一切,以及更多-通用JSON数据验证和方案实施。

答案 2 :(得分:0)

正如我在评论中所说,两个输入文件的内容不是有效的JSON格式。

这里是其中的修改版本,可以进行测试:

suggested.json

{
    "suggested": [
        {
            "city": "Berlin",
            "location": "Europe",
            "hotels": true,
            "restaurants": true,
            "rivers": false
        },
        {
            "city": "Andorra",
            "location": "Global",
            "hotels": false,
            "restaurants": true,
            "rivers": true
        }
    ]
}

master.json

{
    "master": [
        {
            "city": "",
            "location": "Europe",
            "hotels": true,
            "restaurants": false,
            "rivers": false
        }
    ]
}

这是您代码的修改版本,可以按照您想要的方式处理它们:

import json


def openJsonFile(file):
    with open(file) as json_data:
        workData = json.load(json_data)
        return workData


def compareJsonFiles(suggested, master):
    master_dict = master['master'][0]  # Assumes only one object in the list.

    for suggestion in suggested['suggested']:
        print('checking suggestion:', suggestion)
        matches = 0
        for key, value in suggestion.items():
            if key not in master_dict:
                print ("  {0} doesn't have value in {1}".format(key, master_dict))
            else:
                if master_dict[key] == value:
                    print("  for key %r values match" % key)
                    matches += 1
        print('  there were {} matches'.format(matches))


suggested = openJsonFile('suggested.json')
master =  openJsonFile('master.json')

compareJsonFiles(suggested, master)

以下是上面两个文件产生的输出:

checking suggestion: {'city': 'Berlin', 'location': 'Europe', 'hotels': True, 'restaurants': True, 'rivers': False}
  for key 'location' values match
  for key 'hotels' values match
  for key 'rivers' values match
  there were 3 matches
checking suggestion: {'city': 'Andorra', 'location': 'Global', 'hotels': False, 'restaurants': True, 'rivers': True}
  there were 0 matches