FractalLotus
FractalLotus

Reputation: 368

Is there a way to break up long xpath lines in Python?

I've been converting some XPath from my Perl scripts to Python. Everything is working fine but Pylint complains that the lines are too long. In Perl I use white space to make the XPath more readable by breaking up long lines.

Perl example:

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

In Python:

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

Is there a better way in Python? I tried triple quotes but it didn't accept the XPath like that.

Upvotes: 0

Views: 307

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52888

Like mentioned in a comment, just use separate strings...

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

or

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

Upvotes: 3

Related Questions