【问题标题】:Is there a way to break up long xpath lines in Python?有没有办法在 Python 中分解长 xpath 行?
【发布时间】:2021-07-23 15:28:48
【问题描述】:

我一直在将一些 XPath 从我的 Perl 脚本转换为 Python。一切正常,但 Pylint 抱怨线路太长。在 Perl 中,我使用空格通过分解长行来使 XPath 更具可读性。

Perl 示例:

my $node_list = $device->findnodes(
    "./templateObjects
        /TemplateObject[fieldName/text() = 'SCADAArea']
            /keyPairs
                /TemplateKeyValuePair[TemplateName/text() = '%AREA%']
                    /TemplateValue");

在 Python 中:

node_list = device.xpath( "templateObjects/TemplateObject[fieldName/text() = 'SCADAArea']/keyPairs/TemplateKeyValuePair[TemplateName/text() = '%AREA%']/TemplateValue")

在 Python 中有更好的方法吗?我尝试了三引号,但它不接受这样的 XPath。

【问题讨论】:

    标签: python xpath lxml


    【解决方案1】:

    就像评论中提到的那样,只需使用单独的字符串...

    node_list = device.xpath("templateObjects/TemplateObject[fieldName/text() = 'SCADAArea']/keyPairs/"
                             "TemplateKeyValuePair[TemplateName/text() = '%AREA%']/TemplateValue")
    

    node_list = device.xpath("templateObjects"
                             "/TemplateObject[fieldName/text() = 'SCADAArea']"
                             "/keyPairs"
                             "/TemplateKeyValuePair[TemplateName/text() = '%AREA%']"
                             "/TemplateValue")
    

    【讨论】:

      猜你喜欢
      • 2013-05-07
      • 2011-03-03
      • 1970-01-01
      • 2021-07-27
      • 2021-09-21
      • 2010-12-07
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多