比较两个 JSON 文件和 Return 差异
Compare two JSON Files and Return the Difference
我发现了一些与此类似的问题。问题是 none 这些解决方案对我有用,有些太先进了。我正在尝试读取两个 JSON 文件并 return 它们之间的区别。
我希望能够return file2 中缺少的对象并将其写入 file1。
这些都是 JSON 个文件
file1
[
{
"name": "John Wood",
"age": 35,
"country": "USA"
},
{
"name": "Mark Smith",
"age": 30,
"country": "USA"
}
]
.
file2
[
{
"name": "John Wood",
"age": 35,
"country": "USA"
},
{
"name": "Mark Smith",
"age": 30,
"country": "USA"
},
{
"name": "Oscar Bernard",
"age": 25,
"country": "Australia"
}
]
代码
with open("file1.json", "r") as f1:
file1 = f1.read()
item1 = json.loads(file1)
print(item1)
with open("file2.json", "r") as f2:
file2 = f2.read()
item2 = json.loads(file2)
print(item2)
# Returns if that index is the same
for v in range(len(item1)):
for m in range(len(item2)):
if item2[v]["name"] == item1[m]["name"]:
print("Equal")
else:
print("Not Equal")
我不习惯使用 JSON 编程或比较两个不同的文件。
我想帮助 return 区别和基本上复制和粘贴
将丢失的对象放入 file1。我这里显示的是 file1 中的每个对象
等于或不等于 file2。因此,return输出“等于”和“不等于”。
输出:
Equal
Not Equal
Not Equal
Not Equal
Equal
Not Equal
我想 return 如果 file1 不等于 file2 和哪个对象 ("name")
是失踪的那个。之后,我希望能够 add/copy 使用 with open("file2.json", "w").
将丢失的对象放入 file2
with open("file1.json", "r") as f1:
file1 = json.loads(f1.read())
with open("file2.json", "r") as f2:
file2 = json.loads(f2.read())
for item in file2:
if item not in file1:
print(f"Found difference: {item}")
file1.append(item)
print(f"New file1: {file1}")
file1=[
{
"name": "John Wood",
"age": 35,
"country": "USA"
},
{
"name": "Mark Smith",
"age": 30,
"country": "USA"
}
]
file2=[
{
"name": "John Wood",
"age": 35,
"country": "USA"
},
{
"name": "Mark Smith",
"age": 30,
"country": "USA"
},
{
"name": "Oscar Bernard",
"age": 25,
"country": "Australia"
}
]
for item in file2:
if item['name'] not in [x['name'] for x in file1]:
print(f"Found difference: {item}")
file1.append(item)
print(f"New file1: {file1}")
我发现了一些与此类似的问题。问题是 none 这些解决方案对我有用,有些太先进了。我正在尝试读取两个 JSON 文件并 return 它们之间的区别。
我希望能够return file2 中缺少的对象并将其写入 file1。
这些都是 JSON 个文件
file1
[
{
"name": "John Wood",
"age": 35,
"country": "USA"
},
{
"name": "Mark Smith",
"age": 30,
"country": "USA"
}
]
.
file2
[
{
"name": "John Wood",
"age": 35,
"country": "USA"
},
{
"name": "Mark Smith",
"age": 30,
"country": "USA"
},
{
"name": "Oscar Bernard",
"age": 25,
"country": "Australia"
}
]
代码
with open("file1.json", "r") as f1:
file1 = f1.read()
item1 = json.loads(file1)
print(item1)
with open("file2.json", "r") as f2:
file2 = f2.read()
item2 = json.loads(file2)
print(item2)
# Returns if that index is the same
for v in range(len(item1)):
for m in range(len(item2)):
if item2[v]["name"] == item1[m]["name"]:
print("Equal")
else:
print("Not Equal")
我不习惯使用 JSON 编程或比较两个不同的文件。 我想帮助 return 区别和基本上复制和粘贴 将丢失的对象放入 file1。我这里显示的是 file1 中的每个对象 等于或不等于 file2。因此,return输出“等于”和“不等于”。
输出:
Equal
Not Equal
Not Equal
Not Equal
Equal
Not Equal
我想 return 如果 file1 不等于 file2 和哪个对象 ("name") 是失踪的那个。之后,我希望能够 add/copy 使用 with open("file2.json", "w").
将丢失的对象放入 file2with open("file1.json", "r") as f1:
file1 = json.loads(f1.read())
with open("file2.json", "r") as f2:
file2 = json.loads(f2.read())
for item in file2:
if item not in file1:
print(f"Found difference: {item}")
file1.append(item)
print(f"New file1: {file1}")
file1=[
{
"name": "John Wood",
"age": 35,
"country": "USA"
},
{
"name": "Mark Smith",
"age": 30,
"country": "USA"
}
]
file2=[
{
"name": "John Wood",
"age": 35,
"country": "USA"
},
{
"name": "Mark Smith",
"age": 30,
"country": "USA"
},
{
"name": "Oscar Bernard",
"age": 25,
"country": "Australia"
}
]
for item in file2:
if item['name'] not in [x['name'] for x in file1]:
print(f"Found difference: {item}")
file1.append(item)
print(f"New file1: {file1}")