vpd vpd - 21 days ago 5
Python Question

Iterate over python dictionary to retreive only required rows

I am getting the data in HTML table format from external source -

from xml.etree import ElementTree as ET

s = """<table>
<tr><th>Release</th><th>REFDB</th><th>URL</th></tr>
<tr><td>3.7.3</td><td>12345</td><td>http://google.com</td></tr>
<tr><td>3.7.4</td><td>456789</td><td>http://foo.com</td></tr>
</table>
"""


For converting html table to dictionary

table = ET.XML(s)
rows = iter(table)
headers = [col.text for col in next(rows)]
for row in rows:
values = [col.text for col in row]
out = dict(zip(headers, values))


now my expected output is as below given that I will pass the Release version from command line argument.
$ python myscript.py 3.7.3 (I have a code for this)
I am looking for a solution to loop over the dictionary when it finds the particular Release Version - in my case it is 3.7.3

Release Version - 3.7.3
REFDB - 12345
URL - http://google.com

Answer

You don't need a dictionary. Just parse each row's content and see if release version matches your input:

#coding:utf-8

import sys
from lxml import html

if len(sys.argv) != 2:
    raise Exception("Please provide release version only")

release_input = sys.argv[1].strip()

data = """<table>
  <tr><th>Release</th><th>REFDB</th><th>URL</th></tr>
  <tr><td>3.7.3</td><td>12345</td><td>http://google.com</td></tr>
  <tr><td>3.7.4</td><td>456789</td><td>http://foo.com</td></tr>
</table>
"""

tree = html.fromstring(data)
for row in tree.xpath('//tr')[1:]:
    release, refbd, url = row.xpath('.//td/text()')
    if release_input == release:
        print("Release Version - {}".format(release))
        print("REFBD - {}".format(refbd))
        print("URL - {}".format(url))
        break

print("{} release version wasn't found".format(release_input))