【发布时间】:2017-10-27 23:02:07
【问题描述】:
Python 新手:
默认.json
{
"name": {
"provide": ""
},
"test": {
"Fail": {
"centers": None,
"Nearest": 0
},
"far": "",
"Meta": null,
"Only": false,
"Tags": null
},
"Session": "",
"conf": {
"check": "",
"Reg": ""
},
"Token": ""
}
Remote.json
[ {
'name': {
'provide': ''
},
'Name': 'abc',
'test': {
'Service': 'redis',
'Tags': [
'stage'
],
'Fail': {
'centers': None,
'Nearest': 3
},
'Only': false,
'far': '',
'Meta': null
},
'Token': '',
'Session': '',
'conf': {
'Reg': '',
'check': 'name_prefix_match'
},
} ]
我有一个 default.json 和 remote .json。我想要实现的任务是 从 删除所有 json 元素 strong>remote.json 的 remote.json 的值与 default.json 匹配的对象。例如由于 name 的键、值default.json 中的 :{provider=""} 匹配 remote.json 中的 name:{provider=""} .它应该从 remote.json
中删除with open(remote.json) as f:
with open(default.json) as m:
file=json.load(f)
default=json.load(m)
for i in xrange(len(file)):
for key,value in default.items():
#default[key]=value
#a=filter(lambda x: x[""],file.keys())
1.我不知道如何从 default 获取密钥、值并将其与 file 进行比较?任何帮助将不胜感激。
我需要从 remote.json 中删除元素的原因是因为我需要将生成的 json 与其他 json 文件“local.json”进行比较。如果我不删除键,值“”或 null 或 None那么 remote.json 和 local.json 之间的比较永远不会相等。
2.有没有更好的办法解决这个问题?
local.json
{
"Name": "",
"conf": {
"check": "name_prefix_match",
},
"test": {
"Service": "redis",
"Fail": {
"Near": 3
},
"Tags": ""
}
}
【问题讨论】:
-
顺便说一句,您的 JSON 无效:
None不是null和False不是false -
子词典也应该被过滤?
-
请添加过滤后的远程 JSON 示例
-
在您的示例中添加了假设过滤子词典(如
'test')的修复
标签: python json parsing dictionary