'Python -Rest API and List Comprehension
I wondered if someone could cast their eyes over this code I am trying to write and having some issues, I have probably made a mistake somewhere but cannot seem to figure out where I am going wrong.
this data is from a rest api query, therefore I want to extract information from it if possible.
data = {'l3extOut': {'attributes': {'annotation': '', 'dn': 'uni/tn-N4-CUST-TEST/out-L3OUT'}},
'l3extOut': {'attributes': {'uid': '500', 'dn': 'uni/tn-N4-CUST-TEST_DEV/out-L3OUT'}},
'l3extLNodeP': {'attributes': {'annotation': '', 'dn': 'uni/tn-N4-CUST-TEST/out-L3OUT/lnodep-NODES', 'name': 'NODES'}},
'l3extRsNodeL3OutAtt': {'attributes': {'annotation': '', 'dn': 'uni/tn-N4-CUST-TEST/out-L3OUT/lnodep-NODES/rsnodeL3OutAtt-[topology/pod-1/node-101]', 'rType': 'mo', 'rtrId': '1.1.1.101', 'rtrIdLoopBack': 'yes'}}}
print(type(data))
This is a dict when ran in the terminal.
what I am trying to achieve is that I need to be able to access information from the headers below.
l3extOut
l3extLNodeP
l3extRsNodeL3OutAtt
For example, within the list l3extOut, I need the DN value and UID
for example within the list l3extLNodeP, I need the name
for example within the list l3extRsNodeL3OutAtt, I need the DN value
I tried using the below thinking I could just extract the information that I wanted and this works if the headers are the same value but they are not in this case. Is there a better way of achieving what I need
data_list = []
for x in data:
dn = x['l3extOut']['attributes']['dn']
uid = x['l3extOut']['attributes']['uid']
node = x ['l3extLNodeP']['attributes']['name']
path = x ['l3extRsNodeL3OutAtt']['attributes']['dn']
output = f'{dn} {uid} {node} {path}'
data_list.append(f'{dn} {uid} {node} {path} ')
print(data_list)
dn = x['l3extOut']['attributes']['dn'] TypeError: string indices must be integers
Solution 1:[1]
For your method 1, make the below change in your list comprehension:
dn = [data[path]['attributes']['dn'] for path in data]
print(dn)
# result shows
# ['uni/tn-common/out-default']
For your method 2, make the below change in your for loop:
data_list = []
for x in data:
dn_new = data[x]['attributes']['dn']
uid = data[x]['attributes']['uid']
output = f'{dn_new} {uid}'
data_list.append(f'{dn_new} {uid} ')
print(data_list)
# result shows
# ['uni/tn-common/out-default 0 ']
Actually for path in data: and for x in data:, both will return the dictionary key 'l3extOut'.
Solution 2:[2]
The top level key in your data dictionary seems to be irrelevant. Therefore:
data = {'l3extOut': {'attributes': {'annotation': '', 'childAction': '', 'descr': '', 'dn': 'uni/tn-common/out-default', 'enforceRtctrl': 'export', 'extMngdBy': '', 'lcOwn': 'local', 'modTs': '2022-03-24T22:13:00.281+01:00', 'monPolDn': 'uni/tn-common/monepg-default', 'mplsEnabled': 'no', 'name': 'default', 'nameAlias': '', 'ownerKey': '', 'ownerTag': '', 'status': '', 'targetDscp': 'unspecified', 'uid': '0', 'userdom': 'all'}}}
for v in data.values():
print(v['attributes']['dn'])
This allows for multiple keys at the same level as 'l3extOut' but assumes that the associated values have the same structure as shown in the sample data and that the 'attributes' and 'dn' keys exist within those values
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Albert Winestein |
