Muhammad Waqar
Muhammad Waqar

Reputation: 879

Check if tags are empty

I have an XML file that I am parsing through BeautifulSoup. A small portion of my file is:

<document>
    <ad>
        <date>21-Apr-2013</date>
    </ad>
    <ad>
        <date></date>
    </ad>
</document>

What is the fastest way to count the number of date elements that are not empty? Will this be faster if I convert date to an attribute of ad?

Upvotes: 3

Views: 3361

Answers (1)

Jakub M.
Jakub M.

Reputation: 33817

This one will count empty <date> tags:

sum(1 for s in soup.find_all('date') if s.text)

But if you really go for speed, consider some other parser, e.g. SAX

To match attributes, use find_all('ad', attrs={'date': ''})

Upvotes: 3

Related Questions