Home > other >  In the LXML can module variables into the finall path expression
In the LXML can module variables into the finall path expression

Time:09-27

When used in python to parse the XML files such as topic, need according to the content of different field inquiry and the matching, need to put into field endures variable, and then check in accordance with

For example:
(1) the incoming content is "test"
(2) to "test" assigned to the variable
A="test"
(3) put a in the predicate, do some conditions with the aid of the filter, but complains
Root. The.findall ('.//service - identification/header - rule [name=a] ')


How to solve the problem, or is there no other way of thinking to solve (only know python, hoping to find solutions in citing languages),

Due to the beginner, now think of way is to do a lot of the if judgment, but this way is too complex, too low,,,,,

CodePudding user response:

String concatenation can ah

CodePudding user response:

As the program runs, the incoming string (" test ") will change, so can't directly connected to "test" string,

CodePudding user response:

Root. The.findall ('.//service - identification/header - rule [name={}] '. The format (a))

CodePudding user response:

Root. The.findall ('.//service - identification/header - rule [name={}] '. The format (a))
Validation doesn't work,,,,,,,,



In [19] : root. The.findall ('.//service - identification/header - rule [name={}] '. The format (a))
Traceback (the most recent call last) :

The File "c: \ users \ eshicyu \ appdata \ local \ designed \ python \ python38 \ lib \ site - packages \ IPython \ core \ interactiveshell py", line 3331, in run_code
Exec (code_obj, self user_global_ns, self user_ns)

File "
Root. The.findall ('.//service - identification/header - rule [name={}] '. The format (a))

The File "SRC \ LXML \ etree pyx", line 1558, in LXML. Etree. _Element. The.findall

The File "SRC \ LXML \ _elementpath py", line 334, in LXML. _elementpath. The.findall

The File "SRC \ LXML \ _elementpath py", line 312, in LXML. _elementpath. Iterfind

The File "SRC \ LXML \ _elementpath py", line 295, in LXML. _elementpath. _build_path_iterator

The File "SRC \ LXML \ _elementpath py", line 237, in LXML. _elementpath. Prepare_predicate

File "" , the line unknown
SyntaxError: invalid predicate

In [20] : a
The Out [20] : 'NoProxy_12530_01_hr_ip'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

In [21] : root. The.findall (".//service - identification/header - rule [name="NoProxy_12530_0
. : 1 _hr_ip "] ')
Out [21] : [& lt; Element header - rule at 0 x207a4925bc0 & gt;]
  • Related