使用Python / lxml和XPath检索属性名称和值

时间:2017-02-23 10:52:39

标签: python xpath lxml

我正在使用XPath和Python lxml(Python 2)。我对数据进行了两次传递,一次是选择感兴趣的记录,另一次是从数据中提取值。以下是代码类型的示例。

from lxml import etree

xml = """
  <records>
    <row id="1" height="160" weight="80" />
    <row id="2" weight="70" />
    <row id="3" height="140" />
  </records>
"""

parsed = etree.fromstring(xml)
nodes = parsed.xpath('/records/row')
for node in nodes:
    print node.xpath("@id|@height|@weight")

当我运行此脚本时,输出为:

['1', '160', '80']
['2', '70']
['3', '140']

从结果中可以看出,缺少属性的地方,其他属性的位置会发生变化,所以我无法在第2行和第3行判断这是高度还是重量。

有没有办法获取从etree / lxml返回的属性的名称?理想情况下,我应该以格式查看结果:

[('@id', '1'), ('@height', '160'), ('@weight', '80')]

我认识到我可以使用elementtree和Python解决这个特定情况。但是,我希望使用XPath(和相对简单的XPath)来解决这个问题,而不是使用python处理数据。

1 个答案:

答案 0 :(得分:5)

您应该尝试以下操作:

for node in nodes:
    print node.attrib

这会将节点的所有属性的dict返回为{'id': '1', 'weight': '80', 'height': '160'}

如果您想获得[('@id', '1'), ('@height', '160'), ('@weight', '80')]

之类的内容
list_of_attributes = []
for node in nodes:
    attrs = []
    for att in node.attrib:
        attrs.append(("@" + att, node.attrib[att]))
    list_of_attributes.append(attrs)

输出:

[[('@id', '1'), ('@height', '160'), ('@weight', '80')], [('@id', '2'), ('@weight', '70')], [('@id', '3'), ('@height', '140')]]