【问题标题】:How to get nodes matching multiple attributes with specific value?如何让节点匹配具有特定值的多个属性?
【发布时间】:2019-11-07 15:33:54
【问题描述】:

我们可以通过一个属性获取节点:

# root is an instance of etree._Element
nodes = root.findall('person[@city="NY"]')

但是,如何获取多个属性过滤的节点?

nodes = root.findall('person[@city="NY" @gender="M"]')

这不起作用。

【问题讨论】:

    标签: python xml lxml


    【解决方案1】:

    试试这个方法:

    nodes = root.findall('person[@city="NY"][@gender="M"]')
    

    看看它是否有效。

    【讨论】:

    • 感谢您的回答。我以不同的方式解决了这个问题。如果有人可以确认,我会接受作为正确答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 2012-06-05
    • 2023-04-02
    相关资源
    最近更新 更多