1
我有2个JSON文件的键名相同。如何在不覆盖Python的情况下合并这些文件?我已经试过这两种方法:不覆盖JSON文件
z = json_one.copy()
z.update(json_two)
^这会覆盖在json_one数据。
json_one['metros'].append(json_two['metros'])
^这几乎是正确的,但增加了不必要的方括号。
这里是我的2个文件: json_one:
"metros" : [
{
"code" : "SCL" ,
"name" : "Santiago" ,
"country" : "CL" ,
"continent" : "South America" ,
"timezone" : -4 ,
"coordinates" : {"S" : 33, "W" : 71} ,
"population" : 6000000 ,
"region" : 1
} , {
"code" : "LIM" ,
"name" : "Lima" ,
"country" : "PE" ,
"continent" : "South America" ,
"timezone" : -5 ,
"coordinates" : {"S" : 12, "W" : 77} ,
"population" : 9050000 ,
"region" : 1
}
]
json_two:
"metros" : [
{
"code": "CMI",
"name": "Champaign",
"country": "US",
"continent": "North America",
"timezone": -6,
"coordinates": {"W": 88, "N": 40},
"population": 226000,
"region": 1
}
]
文件我要创建的是:
"metros" : [
{
"code" : "SCL" ,
"name" : "Santiago" ,
"country" : "CL" ,
"continent" : "South America" ,
"timezone" : -4 ,
"coordinates" : {"S" : 33, "W" : 71} ,
"population" : 6000000 ,
"region" : 1
} , {
"code" : "LIM" ,
"name" : "Lima" ,
"country" : "PE" ,
"continent" : "South America" ,
"timezone" : -5 ,
"coordinates" : {"S" : 12, "W" : 77} ,
"population" : 9050000 ,
"region" : 1
} , {
"code": "CMI",
"name": "Champaign",
"country": "US",
"continent": "North America",
"timezone": -6,
"coordinates": {"W": 88, "N": 40},
"population": 226000,
"region": 1
}
]
怎么可以这样在做蟒蛇?