迭代 python 字典以仅检索所需的行

Iterate over python dictionary to retrieve only required rows

我正在从外部来源获取 HTML table 格式的数据 -

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>
"""

用于将 html table 转换为字典

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))

现在我的预期输出如下所示,因为我将从命令行参数传递发布版本。 $ python myscript.py 3.7.3(我有一个代码) 我正在寻找一种解决方案,当它找到特定的发行版本时循环遍历字典 - 在我的例子中它是 3.7.3

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

假设每个版本只有一行并且您根本不需要其他版本,您可以创建一个函数来解析 HTML 和 returns dict 表示版本一经发现。如果找不到版本,它可以 return None 代替:

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>
"""

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'

输出:

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

你不需要字典。只需解析每一行的内容并查看发布版本是否与您的输入匹配:

#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))
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>
"""

table = ET.XML(s)
rows = iter(table)
headers = [col.text for col in next(rows)]
master = {}

for row in rows:
    values = [col.text for col in row]
    out = dict(zip(headers, values))
    if 'Release' in out:
        master[out['Release']] = out

# Use the release to get the right dict out of master
print(master)
if in_data in master:
    for k, v in master[in_data]:
        # print here
        pass
else:
    print('Error')
import lxml.html
from collections import namedtuple
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>
  <tr><td>3.7.5</td><td>151515</td><td>http://foo.com</td></tr>
</table>
"""
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

输出:

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

如果您将字典累积在一个列表中:

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

您可以过滤列表 -

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]

然后打印所有通过过滤器的词典 -

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