尝试在python中解析Json结构并使用键“ cat”添加新值:
data = []
for x in a:
for y in x['Hp'].values():
for z in y:
for k in z['abc']['xyz']:
for m in data:
det = m['response']
// Some processing with det whose output is stored in s
k['cat'] = s
print x
但是,当打印x时,仅将最后一个值附加到整个字典中,而s则具有不同的值。 很明显,每次循环时都会覆盖“ cat”键,但找不到正确的方法
下面是一个示例Json结构:
{
"_id" : ObjectId("asdasda156121s"),
"Hp" : {
"bermud" : [
{
"abc" : {
"gfh" : 1,
"fgh" : 0.0,
"xyz" : [
{
"kjl" : "0",
"bnv" : 0,
}
],
"xvc" : "bv",
"hgth" : "INnn",
"sdf" : 0,
}
}
},
{
"abc" : {
"gfh" : 1,
"fgh" : 0.0,
"xyz" : [
{
"kjl" : "0",
"bnv" : 0,
}
],
"xvc" : "bv",
"hgth" : "INnn",
"sdf" : 0,
}
}
},
..
答案 0 :(得分:1)
如果要存储所有值更改
k['cat'] = s
到
if 'cat' in k.keys():
k['cat'] += s
else:
k['cat'] = s
如果您只想存储第一个更改
k['cat'] = s
到
if 'cat' not in k.keys():
k['cat'] = s