迭代python字典以仅检索所需的行 [英] Iterate over python dictionary to retrieve only required rows

查看:29
本文介绍了迭代python字典以仅检索所需的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从外部来源获取 HTML 表格格式的数据 -

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

用于将html表格转换为字典

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

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

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

推荐答案

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

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

这篇关于迭代python字典以仅检索所需的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆