我需要修改以下json文件test.json:
{
"install": {
"site": {
"acls": {
"dns": {
"authorized_ports": ["53:tcp", "53:udp"]
}
},
"network": {
"clusters": {
"__ip_range_1__": {
"dhcpstart": "__ip__",
"dhcpend": "__ip__",
"adminip": "__ip__"
},
"__ip_range_2__": {
"dhcpstart": "__ip__",
"dhcpend": "__ip__",
"adminip": "__ip__"
}
}
}
}
}
}
以上是缩写,原始文件中有更多条目。我每个站点都有几个这样的文件,因此__ip_range_x__
在每个文件中都是不同的,因此每个IP都是如此。我需要为每个__ip_range_x__
元素添加条目。新条目是存储在mod.json中的词典字典(下面的interface_config
):
{
"path": "{install}{site}{network}{clusters}{*}",
"install" : {
"site": {
"network": {
"clusters": {
"__iprange": {
"interface_config": {
"framesize": "1500",
"framesize_vm": "1500"
}
}
}
}
}
}
}
我还需要在原始json文件的不同部分添加其他条目。
现在,我只是想迭代test.json中的所有元素。最后,我想构建一个指向test.json中每个元素的路径,并将其与mod.json中的路径进行匹配,以修改test.json。但是,我无法获取原始文件中打印的所有元素。我目前的代码:
import json
import pprint
def traverse(d, path=None):
if path is None:
path = []
for item,val in d.iteritems():
if isinstance(item, dict):
for k,v in item.iteritems():
print k
traverse(v)
elif isinstance(item, list):
for j in item:
(traverse(j))
else:
print item
if isinstance(val, dict):
for k,v in val.iteritems():
print k
traverse(v)
elif isinstance(val, list):
for j in val:
(traverse(j))
with open("test.json", "r") as jf:
data = json.load(jf)
traverse(data)
以上的输出是:
$ ./now.py
install
site
acls
dns
authorized_ports
Traceback (most recent call last):
File "./now.py", line 51, in <module>
traverse(data)
File "./now.py", line 23, in traverse
traverse(v)
File "./now.py", line 23, in traverse
traverse(v)
File "./now.py", line 26, in traverse
(traverse(j))
File "./now.py", line 9, in traverse
for item,val in d.iteritems():
AttributeError: 'unicode' object has no attribute 'iteritems'
我知道我的第一个iteritems
电话是在错误的地方,但我无法理解递归...任何指针都赞赏。顺便说一下,我在Python 2上。
修改
我正在尝试处理的实际json比上面列出的更复杂。这是一个经过编辑的版本:
{
"install": {
"site": {
"acls": {
"dns": {
"authorized_ports": ["53:tcp", "53:udp"]
}
},
"network": {
"clusters": {
"__ip_range_1__": {
"dhcpstart": "__ip__",
"dhcpend": "__ip__",
"adminip": "__ip__"
},
"__ip_range_2__": {
"dhcpstart": "__ip__",
"dhcpend": "__ip__",
"adminip": "__ip__"
}
}
}
}
}
"config": {
"ippool": [
{
"pool_name": "/ippool1",
"pool_description": "IP Pool1",
"ranges": [["__ip__", "__ip__"]]
},
{
"pool_name": "/ippool2",
"pool_description": "IP Pool2",
"ranges": [["__ip__", "__ip__"]]
}
],
"storage": [
{
"account": "/root",
"credentials": {
"account": "admin",
"service": "storage",
"user": "admin",
"password": "pass"
}
}
]
}
}
我修改了Paul Panzer的答案,列出如下列表:
def traverse(d, path=[]):
for k, v in d.iteritems():
yield path + [k], v
if isinstance(v, dict):
for k,v in traverse(v, path + [k]):
yield k,v
elif isinstance(v, list):
for k in v:
traverse(k, path + [])
但是上面的内容不会打印ippool和存储列表中的元素。一旦遇到字典列表,就不会出于某种原因遍历它。
答案 0 :(得分:4)
以下是traverse
例程的清理版本。它只是遍历一个嵌套的字典/列表;为了清晰起见,我已经切断了其他一切。希望它有所帮助。
master = {
"install": {
"site": {
"acls": {
"dns": {
"authorized_ports": ["53:tcp", "53:udp"]
}
},
"network": {
"clusters": {
"__ip_range_1__": {
"dhcpstart": "__ip__",
"dhcpend": "__ip__",
"adminip": "__ip__"
},
"__ip_range_2__": {
"dhcpstart": "__ip__",
"dhcpend": "__ip__",
"adminip": "__ip__"
}
}
}
}
},
"config": {
"ippool": [
{
"pool_name": "/ippool1",
"pool_description": "IP Pool1",
"ranges": [["__ip__", "__ip__"]]
},
{
"pool_name": "/ippool2",
"pool_description": "IP Pool2",
"ranges": [["__ip__", "__ip__"]]
}
],
"storage": [
{
"account": "/root",
"credentials": {
"account": "admin",
"service": "storage",
"user": "admin",
"password": "pass"
}
}
]
}
}
def traverse(dict_or_list, path=[]):
if isinstance(dict_or_list, dict):
iterator = dict_or_list.iteritems()
else:
iterator = enumerate(dict_or_list)
for k, v in iterator:
yield path + [k], v
if isinstance(v, (dict, list)):
for k, v in traverse(v, path + [k]):
yield k, v
for path, node in traverse(master):
print path
输出:
['config']
['config', 'ippool']
['config', 'ippool', 0]
['config', 'ippool', 0, 'ranges']
['config', 'ippool', 0, 'ranges', 0]
['config', 'ippool', 0, 'ranges', 0, 0]
['config', 'ippool', 0, 'ranges', 0, 1]
['config', 'ippool', 0, 'pool_name']
['config', 'ippool', 0, 'pool_description']
['config', 'ippool', 1]
['config', 'ippool', 1, 'ranges']
['config', 'ippool', 1, 'ranges', 0]
['config', 'ippool', 1, 'ranges', 0, 0]
['config', 'ippool', 1, 'ranges', 0, 1]
['config', 'ippool', 1, 'pool_name']
['config', 'ippool', 1, 'pool_description']
['config', 'storage']
['config', 'storage', 0]
['config', 'storage', 0, 'credentials']
['config', 'storage', 0, 'credentials', 'account']
['config', 'storage', 0, 'credentials', 'password']
['config', 'storage', 0, 'credentials', 'user']
['config', 'storage', 0, 'credentials', 'service']
['config', 'storage', 0, 'account']
['install']
['install', 'site']
['install', 'site', 'acls']
['install', 'site', 'acls', 'dns']
['install', 'site', 'acls', 'dns', 'authorized_ports']
['install', 'site', 'acls', 'dns', 'authorized_ports', 0]
['install', 'site', 'acls', 'dns', 'authorized_ports', 1]
['install', 'site', 'network']
['install', 'site', 'network', 'clusters']
['install', 'site', 'network', 'clusters', '__ip_range_2__']
['install', 'site', 'network', 'clusters', '__ip_range_2__', 'dhcpend']
['install', 'site', 'network', 'clusters', '__ip_range_2__', 'adminip']
['install', 'site', 'network', 'clusters', '__ip_range_2__', 'dhcpstart']
['install', 'site', 'network', 'clusters', '__ip_range_1__']
['install', 'site', 'network', 'clusters', '__ip_range_1__', 'dhcpend']
['install', 'site', 'network', 'clusters', '__ip_range_1__', 'adminip']
['install', 'site', 'network', 'clusters', '__ip_range_1__', 'dhcpstart']
答案 1 :(得分:3)
似乎你希望这比必要的更复杂:
with open("test.json", "r") as jf:
data = json.load(jf)
with open("mod.json", "r") as mf:
mod = json.load(mf)
ip_ranges = data['install']['site']['network']['clusters']
for rng, val in mod['install']['site']['network']['clusters'].items():
data[rng]["interface_config"] = val["interface_config"]
答案 2 :(得分:0)
来自AChampion的修改代码,用于在具有动态密钥的字典的所有元素下添加条目:
with open(args.masterjson, "r") as masterjf:
data = json.load(masterjf)
with open(args.modjson, "r") as modjf:
mod = json.load(modjf)
new_value = mod['install']['site']['network']['clusters']['__iprange']['interface_config']
for rng, val in data['install']['site']['network']['clusters'].items():
data['install']['site']['network']['clusters'][rng]["interface_config"] = new_value
上面在群集中的每个ip范围下添加了一个新的词典字典。输出:
master BEFORE update:
{u'dhcpend': u'__ip__', u'adminip': u'__ip__', u'dhcpstart': u'__ip__'}
{u'dhcpend': u'__ip__', u'adminip': u'__ip__', u'dhcpstart': u'__ip__'}
master AFTER update:
{'interface_config': {u'framesize_vm': u'1500', u'framesize': u'1500'}, u'dhcpend': u'__ip__', u'adminip': u'__ip__', u'dhcpstart': u'__ip__'}
{'interface_config': {u'framesize_vm': u'1500', u'framesize': u'1500'}, u'dhcpend': u'__ip__', u'adminip': u'__ip__', u'dhcpstart': u'__ip__'}