简体   繁体   中英

Adding an OR condition in Findall, Lxml

I have the following findall expression :

for r in p.findall('.//r'):
                 for a in r.findall('.//br'):
                    text+= " "
                 for c in r.findall('.//tab'):
                     text+= " "  

And i want to add a space in the text variable if i come across the tag "br" or "tab" , but I want to use a single expression rather than 2 separate ones. something like:

for a in r.findall('.//br'|'.//tab'):

but this returns an unsupported operand type error.

TypeError: unsupported operand type(s) for |: 'str' and 'str'

What is the right syntax for this?

The code is using | operator for two string operands.

>>> 'a' | 'b'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'str' and 'str'

Specify | inside the string literal. And use xpath method :

for a in r.xpath('.//br|.//tab'):

If you want to use findall , concatenate two list into one and iterate it:

for a in r.findall('.//br') + r.findall('.//table'):

or using itertools.chain :

import itertools

for a in itertools.chain(r.findall('.//br'), r.findall('.//table')):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM