Iterate over python dictionary to retrieve only required rows

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

    import lxml.html
    from collections import namedtuple
    s = """
    ReleaseREFDBURL
    3.7.312345http://google.com
    3.7.4456789http://foo.com
    3.7.5151515http://foo.com
    """ def info_gen(rows): info = namedtuple('info', ['Release', 'REFDB', 'URL']) for row in rows: yield info(*row.xpath('.//text()')) html = lxml.html.fromstring(s) rows = html.xpath('//table//tr[td]') Release = input("Enter Release:") for info in info_gen(rows): if Release in info: print(info) break

    out:

     Enter Release:3.7.5
    info(Release='3.7.5', REFDB='151515', URL='http://foo.com')
    

提交回复
热议问题