Iterate over python dictionary to retrieve only required rows

后端 未结 5 1022
长发绾君心
长发绾君心 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:22

    If you accumulate the dictionaries in a list:

    result = []
    for row in rows:
        values = [col.text for col in row]
        result.append(dict(zip(headers, values)))
    

    You can filter the list -

    import operator
    value = '3.7.3'
    release = operator.itemgetter('Release')
    refdb = operator.itemgetter('REFDB')
    url = operator.itemgetter('URL')
    data = [d for d in result if release(d) == value]
    

    Then print all the dictionaries that got past the filter -

    f_string = 'Release Version - {}\nREFDB - {}\nURL - {}'
    for d in data:
        print(f_string.format(release(d), refdb(d), url(d)))
    

提交回复
热议问题