Python Forum
get specific items from result return info
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get specific items from result return info
#1
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
Reply
#2
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'
Reply
#3
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()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thank you, that solved it for me.

regards
Mamoman
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  This result object does not return rows. It has been closed automatically dawid294 6 1,091 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  What is all the info in the info window in Idle? Pedroski55 3 713 Jul-08-2023, 11:26 AM
Last Post: DeaD_EyE
  how do I return Max Test result + corresponding student name from an excel dataset? sean1 3 1,279 Jan-16-2022, 09:07 PM
Last Post: snippsat
Question How to gather specific second-level items from a list chatguy 2 1,564 Dec-17-2021, 05:05 PM
Last Post: chatguy
  Delete specific lines contain specific words mannyi 2 4,150 Nov-04-2019, 04:50 PM
Last Post: mannyi
  python code (embedded in Redshift) to return result of the query Mel 0 2,463 Aug-24-2018, 06:12 PM
Last Post: Mel
  Multiple "return" statements or "result" variable? WolfWayfarer 7 7,795 Jul-25-2018, 07:51 AM
Last Post: perfringo
  Picking A number of items from a dict and getting there info TFish 2 2,773 Jan-17-2018, 08:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020