Python Forum

Full Version: get specific items from result return info
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def get_all_aps(self):
    
        result = self.session.get(self.api_get_ap_list)
        print result
        #print result[list]
        totalcount = result["totalCount"]
        
        print totalcount
so when i run this, it nicely prints the totalcount from this result output
{u'firstIndex': 0, u'totalCount': 3189, u'list': [{u'name': u'PSK-AP325', u'mac': u'AA:AA:AA:AA:8E:70', u'serial': u'131424000'}]}

what do i need to do to get each item in the list part.
So I want to get the value of PSK-AP325 and 131424000 from the result output.

Any ideas?
Mamoman
You have a dict inside of a list inside of a dict, so you'll need to index it 3 times:
>>> pp(result)
{
    'firstIndex': 0,
    'totalCount': 3189,
    'list': [
        {
            'name': 'PSK-AP325',
            'mac': 'AA:AA:AA:AA:8E:70',
            'serial': '131424000'
        }
    ]
}
>>> result['list'][0]['name']
'PSK-AP325'
>>> result['list'][0]['serial']
'131424000'
result is json. so

for item in result['list']:
    print('{name}, {serial}'.format(**item))
or

for item in result['list']:
    for key, value in item.items():
        print('{} --> {}'.format(key, value))
    print()
Thank you, that solved it for me.

regards
Mamoman