简体   繁体   English

将append和python中的json文件进行比较

[英]Compare and append to json file in python

I have 2 files like these:我有 2 个这样的文件:

file1.json文件1.json

[
  {
    "name": "john",
    "version": "0.0"
  },
  {
    "name": "peter",
    "version": "1.0"
  },
  {
    "name": "bob",
    "version": "2.0",
    "single": "true"
  }
]

file2.json文件2.json

[
  {
    "name": "jane",
    "version": "0.0"
  },
  {
    "name": "peter",
    "version": "1.0"
  },
  {
    "name": "bob",
    "version": "2.0",
    "single": "true"
  }
]

I want to compare the "name" values in file1.json with the "name" values in file2.json.我想将 file1.json 中的“名称”值与 file2.json 中的“名称”值进行比较。 If file2.json has some "name" values not in file1.json, I want to append that json object to file1.json.如果 file2.json 有一些不在 file1.json 中的“名称”值,我想 append 即 json object 到 file1.json。

For the above 2 files, I want file1 to be appended with the "name": "jane" object since that is not present in file1.对于以上 2 个文件,我希望 file1 附加 "name": "jane" object 因为它不存在于 file1 中。 So file1 should be updated to:所以 file1 应该更新为:

[
  {
    "name": "john",
    "version": "0.0"
  },
  {
    "name": "peter",
    "version": "1.0"
  },
  {
    "name": "bob",
    "version": "2.0",
    "single": "true"
  },
  {
    "name": "jane",
    "version": "0.0"
  }
]

What I have tried:我试过的:

with open('file1.json', 'r') as file1, open('file2.json', 'r') as file2:
    file1_json = json.load(file1)
    file2_json = json.load(file2)

for object in file2_json:
    if object['name'] not in file1_json.values():
        file1_json.update(object)

with open('file1.json', 'w') as file1:
    json.dump(file1_json, file1, indent=4)

Collect the names in file1_json , find the missing names in file2_json , then add them to file1_json :收集 file1_json 中的名称,找到file1_json中缺少的名称,然后将它们添加到file2_json file1_json

import json

with open('file1.json', 'r') as file1, open('file2.json', 'r') as file2:
    file1_json = json.load(file1)
    file2_json = json.load(file2)

names = [o['name'] for o in file1_json]
missing = [o for o in file2_json if o['name'] not in names]
file1_json.extend(missing)

with open('file1.json', 'w') as file1:
    json.dump(file1_json, file1, indent=2)

Output: Output:

[
  {
    "name": "john",
    "version": "0.0"
  },
  {
    "name": "peter",
    "version": "1.0"
  },
  {
    "name": "bob",
    "version": "2.0",
    "single": "true"
  },
  {
    "name": "jane",
    "version": "0.0"
  }
]

This should work if all dictionary values are immutable (which they are):如果所有字典值都是不可变的(它们是),这应该有效:

with open('file1.json', 'r') as file1, open('file2.json', 'r') as file2:
    file1_json = json.load(file1)
    file2_json = json.load(file2)
print([dict(s) for s in set(frozenset(d.items()) for d in file1_json+file2_json)])

You could create an index into json2 to speed lookups.您可以在 json2 中创建索引以加快查找速度。 Then take the set of names from each and subtract.然后从每个名称中取出一组名称并减去。 The result is the missing names.结果是缺少名称。 Use the index to add the missing values.使用索引添加缺失值。

with open('file1.json', 'r') as file1, open('file2.json', 'r') as file2:
    file1_json = json.load(file1)
    file2_json = json.load(file2)

idx2 = {d["name"]:d for d in file2_json}
missing = set(idx2) - set(d["name"] for d in file1_json)
file1_json.extend(idx2[name] for name in missing)

I believe what you want is to merge dictionaries essentially.我相信你想要的是本质上合并字典。 The fact it comes from a json does not really matter here.它来自 json 的事实在这里并不重要。 Take a look at this.看看这个。 https://stackoverflow.com/a/62820532/4944708 https://stackoverflow.com/a/62820532/4944708

Here's the full solution, assuming you have read the jsons in:这是完整的解决方案,假设您已阅读以下 jsons:

def to_dict(json_):
     return {item['name']: item for item in json_}

list({**to_dict(json1), **to_dict(json2)}.values())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM