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
}
]
怎麼可以這樣在做蟒蛇?