【发布时间】:2017-10-17 10:47:48
【问题描述】:
我有多个包含大写字母和国家/地区的 JSON 文件。如何从所有文件中删除重复的键值对?
我有以下 JSON 文件之一
{
"data": [
{
"Capital": "Berlin",
"Country": "Germany"
},
{
"Capital": "New Delhi",
"Country": "India"
},
{
"Capital": "Canberra",
"Country": "Australia"
},
{
"Capital": "Beijing.",
"Country": "China"
},
{
"Capital": "Tokyo",
"Country": "Japan"
},
{
"Capital": "Tokyo",
"Country": "Japan"
},
{
"Capital": "Berlin",
"Country": "Germany"
},
{
"Capital": "Moscow",
"Country": "Russia"
},
{
"Capital": "New Delhi",
"Country": "India"
},
{
"Capital": "Ottawa",
"Country": "Canada"
}
]
}
有很多这样的 JSON 文件包含重复的项目。如何删除重复的项目,只保留第一次出现?我试过这个,但不起作用
dupes = []
for f in json_files:
with open(f) as json_data:
nations = json.load(json_data)['data']
#takes care of duplicates and stores it in dupes
dupes.append(x for x in nations if x['Capital'] in seen or seen.add(x['Capital']))
nations = [x for x in nations if x not in dupes] #want to keep the first occurance of the item present in dupes
with open(f, 'w') as json_data:
json.dump({'data': nations}, json_data)
【问题讨论】:
标签: json python-3.x