Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'tutorial': 0.03; 'output': 0.05; 'true,': 0.05; 'interpreter.': 0.07; 'none,': 0.07; 'puts': 0.07; 'variables': 0.07; "'name':": 0.09; 'executes': 0.09; 'false,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'structure,': 0.09; 'subject:parsing': 0.09; 'teh': 0.09; 'tutorials,': 0.09; 'api': 0.11; 'subject:Help': 0.11; 'python': 0.11; '(palo': 0.16; ':-d': 0.16; 'dict': 0.16; 'dump': 0.16; 'list"': 0.16; 'nick': 0.16; 'palo': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'stepping': 0.16; 'subject:API': 0.16; 'index': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'looked': 0.18; "skip:' 30": 0.19; 'slightly': 0.19; 'seems': 0.21; 'command': 0.22; '>>>': 0.22; 'example': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'days,': 0.24; 'entries': 0.24; 'parse': 0.24; 'simpler': 0.24; 'question': 0.24; 'script': 0.25; 'task': 0.26; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'correct': 0.29; 'rest': 0.29; '(this': 0.29; 'xml': 0.29; 'related': 0.29; 'change,': 0.30; 'list:': 0.30; 'reaches': 0.30; 'code': 0.31; 'easier': 0.31; "skip:' 10": 0.31; 'firewall': 0.31; 'invoke': 0.31; "skip:' 40": 0.31; 'probably': 0.32; 'another': 0.32; 'maybe': 0.34; 'skip:d 20': 0.34; 'subject:from': 0.34; 'subject:with': 0.35; 'tool': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'doing': 0.36; 'next': 0.36; 'entry': 0.36; 'useful': 0.36; 'subject:?': 0.36; 'should': 0.36; 'list': 0.37; 'easily': 0.37; 'manager': 0.38; 'displays': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'ability': 0.39; 'does': 0.39; 'structure': 0.39; 'use.': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'devices': 0.61; 'field.': 0.61; 'new': 0.61; 'simple': 0.61; 'information': 0.63; 'name': 0.63; 'central': 0.64; 'management': 0.65; 'world': 0.66; 'here': 0.66; 'a/v': 0.84; 'alto': 0.84; 'appreciative': 0.84; 'believe,': 0.84; 'dict.': 0.84; 'firewalls': 0.84; 'grabbing': 0.84; 'outsourced': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Help with parsing a dict from Vendor's API? Date: Wed, 15 Oct 2014 11:22:26 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd904a.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 122 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413364963 news.xs4all.nl 2937 [2001:888:2000:d::a6]:45997 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 349492569 X-Received-Bytes: 8334 Xref: csiph.com comp.lang.python:79145 Nick Ellson wrote: > Hello! > > I have a very specific question related to the output of a Vendors API > (Palo Alto Networks "pan.xapi" and how I might farm data from this output. > I am new to python, doing well in the tutorials, but this is an automation > task at work and I know the rest will be much easier once i get past the > ability to read this dict. > > The code reaches in to the central Palo Alto firewall manager (Panorama) > and executes a simple command to return all of the information for each of > the managed firewalls in the field. It captured this output in XML I > believe, but has the ability to return it in python dict format too, which > looked like probably the best format to use. Here is the test I tried > > > xapi.op(cmd='show devices connected', cmd_xml=True ) > MyDict=xapi.xml_python() > print (type(MyDict)) > print (MyDict) > > > and I get: (This displays only 2 firewalls of the 180, so you can see the > structure, and that python does say it is a "dict") > > bertha bin # ./test.py > > {'response': {'result': {'devices': {'entry': [{'av-version': '1391-1863', > 'unsupported-version': False, 'ip-address': '1.8.2.8', 'sw-version': > '4.1.9', 'vsys': {'entry': [{'name': 'vsys1', 'shared-policy-md5sum': > '8a8dcd146e24bd750ae571059bc09210', 'shared-policy-status': None, > 'display-name': 'vsys1'}]}, 'uptime': '350 days, 14:29:49', > 'threat-version': '460-2394', 'operational-mode': 'normal', 'multi-vsys': > False, 'global-protect-client-package-version': '0.0.0', 'app-version': > '460-2394', 'model': 'PA-200', 'connected': True, 'name': '001606000002', > 'family': '200', 'url-filtering-version': '4390', 'vpn-disable-mode': > False, 'logdb-version': '4.1.2', 'serial': '001606000002', 'hostname': > 'bob-int-fw'}, {'av-version': '1391-1863', 'unsupported-version': False, > 'ip-address': '1.9.8.8', 'sw-version': '4.1.9', 'vsys': {'entry': > [{'name': 'vsys1', 'shared-policy-md5sum': > '8a8dcd146e24bd750ae571059bc09210', 'shared-policy-status': None, > 'display-name': 'vsys1'}]}, 'uptime': '358 days, 0:03:20', > 'threat-version': '460-2394', 'operational-mode': 'normal', 'multi-vsys': > False, 'global-protect-client-package-version': '0.0.0', 'app-version': > '460-2394', 'model': 'PA-200', 'connected': True, 'name': '001606000009', > 'family': '200', 'url-filtering-version': '4390', 'vpn-disable-mode': > False, 'logdb-version': '4.1.2', 'serial': '001606008639', 'hostname': > 'bib-int-fw'}, <****repeats for 180 firewalls****> ]}}, 'status': > 'success'}} > > > What I want is to parse through each firewall grabbing the "ip-address" > value so that I can dump it to a list: > > > > > > For use in another network management tool so I don't rely on outsourced > help to remember to place teh firewalls into the correct tools. > > But dang if every dict tutorial seems to deal with slightly simpler > looking structures than what this puts out. I would be very appreciative > with help stepping out of the 6 line "address book/grocery list" example > world for a taste of something useful :-) > > Maybe to a Python coder, it maybe a simple even be able to randomly > reference a firewall index number and teh value in this structure so one > can easily just pluck any A/V pair at will.. just not for me yet :-D Look at your dict (I use the name 'd' instead of 'MyDict'): d = {"response": {"result": ...} So d["response"] will give you {"result": {"devices": ...} A good tool to explore a data structure like this is the interactive interpreter. If you invoke the script with python -i test.py you have all its global variables available, i. e. >>> MyDict { ... } # you should see the dictionary contents On we go: d["response"]["result"] will give {"devices": {"entry": ...} Next step: d["response"]["result"]["devices"] --> {"entry": [...] That's a list for change, so you loop over it: entries = d["response"]["result"]["devices"]["entry"] for entry in entries: print(entry) # --> {..., 'ip-address': '1.8.2.8', ... } So the final code is: for entry in d["response"]["result"]["devices"]["entry"]: print(entry["ip-address"])