我有一个包含对象数组的 json 文件,文件内的数据如下所示:
[
{‘name’: ‘A’,
‘address’: ‘some address related to A’,
‘details’: ‘some details related to A’},
{‘name’: ‘B’,
‘address’: ‘some address related to A’,
‘details’: ‘some details related to B’},
{‘name’: ‘C’,
‘address’: ‘some address related to A’,
‘details’: ‘some details related to C’}
]
我想删除多余的键值,所以输出应该是这样的:
[
{‘name’: ‘A’,
‘address’: ‘some address related to A’,
‘details’: ‘some details related to A’},
{‘name’: ‘B’,
‘details’: ‘some details related to B’},
{‘name’: ‘C’,
‘details’: ‘some details related to C’}
]
所以,我尝试过这段代码,在此链接中找到了它:
import json
with open(‘./myfile.json’) as fp:
data= fp.read()
unique = []
for n in data:
if all(unique_data["address"] != data for unique_data["address"] in unique):
unique.append(n)
#print(unique)
with open(“./cleanedRedundancy.json”, ‘w’) as f:
f.write(unique)
但它给了我这个错误:
TypeError: string indices must be integers
动漫人物
相关分类