I've a JSON File:
[
{
"schema":{"id":1},
"rawdata":{},
"enriched":{"name":"xy1"}
},
{
"schema":{"id":2},
"rawdata":{},
"enriched":{"name":"xy2"}
},
{ "schma":{"id":3}, "radata":{},"enrichd":{"name":"xy3"}
},
{ "schema":{"id":3}, "radata":{},"enriched":{"name":"xy3"}
},
{"name":"xy200"},
{
"schema":{"id":4},
"rawdata":{},"enriched":{"name":"xy4"}
}
]
where I've to filter the numbers of the nested entries from "name" : "xy1".
In short words: I need every number after "xy".
My code I already wrote is this:
for value in data:
if value["enriched"]:
get_char = value["enriched"]["name"]
num = get_char[2:]
print(f"{num}")
elif value["enrichd"]:
get_char = value["enrichd"]["name"]
num = get_char[2:]
print(f"{num}")
elif value["name"]:
get_char = value["name"]
num = get_char[2:]
print(f"{num}")
else:
print("test")
and my error message is this:
1
2
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-155-13d4f2ca308f> in <module>
52 for value in data:
53 try:
---> 54 if value["enriched"]:
55 get_char = value["enriched"]["name"]
56 num = get_char[2:]
KeyError: 'enriched'
But I dont figure out why I the loop doesnt use the elif's I've wrote.
Maybe someone could help me :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…