Iterate over python dictionary to retrieve only required rows

后端 未结 5 1024
长发绾君心
长发绾君心 2021-01-24 01:42

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

from xml.etree import ElementTree as ET

s = \"\"\"
Release
5条回答
  •  青春惊慌失措
    2021-01-24 02:08

    Assuming that there's only one row per version and you don't need the other versions at all you could create a function that parses the HTML and returns dict representing version as soon as it's found. If version is not found it could return None instead:

    from xml.etree import ElementTree as ET
    
    s = """
    ReleaseREFDBURL
    3.7.312345http://google.com
    3.7.4456789http://foo.com
    """ def find_version(ver): 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)) if out['Release'] == ver: return out return None res = find_version('3.7.3') if res: for x in res.items(): print(' - '.join(x)) else: print 'Version not found'

    Output:

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

提交回复
热议问题