I have run the following Python script to remove duplcates between two json files based on userid:
import json
with open("target_user.json", "r", encoding= utf-8 ) as f1:
target = json.load(f1)
with open("source_user.json", "r", encoding= utf-8 ) as f2:
source = json.load(f2)
target2 = []
for item in target:
if item[ userid ] not in [x[ userid ] for x in source]:
target2.append(item)
with open( target_user2.json , w , encoding= utf-8 ) as nf:
json.dump(target2, nf, indent=4)
造成错误:
TypeError: list indices must be integers or slices, not str
本节内通知:
if item[ userid ] not in [x[ userid ] for x in source]:
我已尝试过以下工作,但没有确定:
if dict([item][ userid ]) not in [dict[x][ userid ] for x in source]:
我的json档案样本:
{
"567897068": {
"userid": "567897068",
"status": "UserStatus.OFFLINE",
"name": "btb appeal",
"bot": false,
"username": "None"
},
"5994781619": {
"userid": "5994781619",
"status": "UserStatus.OFFLINE",
"name": "Ahh",
"bot": false,
"username": "hourng999"
},
"1873973169": {
"userid": "1873973169",
"status": "UserStatus.RECENTLY",
"name": "Chanthy",
"bot": false,
"username": "Alis_Thy"
}
}
感谢任何帮助。
我已尝试了John建议的内容,但没有:
import json
with open("target_user.json", "r", encoding= utf-8 ) as f1:
target = json.load(f1)
with open("source_user.json", "r", encoding= utf-8 ) as f2:
source = json.load(f2)
target2 = []
for item in target.values():
# print(item) # print scanned results on CMD
#if item[ userid ] not in [x[ userid ] for x in source]:
if item[ userid ] not in [x[ userid ] for x in source]:
target2.append(item)
with open( target_user2.json , w , encoding= utf-8 ) as nf:
json.dump(target2, nf, indent=4)