lxml ignore none in getchildren

Peter Otten __peter__ at web.de
Tue Oct 4 08:28:34 EDT 2016


Sayth Renshaw wrote:

> I am following John Shipmans example from
> http://infohost.nmt.edu/~shipman/soft/pylxml/web/Element-getchildren.html
> 
>>>> xml = '''<corral><horse n="2"/><cow n="17"/>
> ...  <cowboy n="2"/></corral>'''
>>>> pen = etree.fromstring(xml)
>>>> penContents = pen.getchildren()
>>>> for  content in penContents:
> ...     print "%-10s %3s" % (content.tag, content.get("n", "0"))
> ...
> horse        2
> cow         17
> cowboy       2
>>>> 
> 
> If I make one minor modification to the xml and change an n to an m as in
> my example below the getchildren will return none for none matches, how
> can I ignore nones?
> 
> In [2]: from lxml import etree
> 
> In [3]: xml = '''<corral><horse n="2"/><cow m="17"/> <cowboy
> n="2"/></corral>'''
> 
> In [4]: pen =etree.fromstring(xml)
> 
> In [5]: pencontents = pen.getchildren()
> 
> In [6]: for content in pencontents:
>    ...:     print(content.get('n'))
> 2
> None
> 2
> 
> Because
> In [17]: for content in pencontents:
>    ....:     if content is not None:
>    ....:         print(content.get('n'))
> 
> Sayth

The obvious fix would be to check for None in the right place:

>>> for content in pen.getchildren():
...     n = content.get("n")
...     if n is not None:
...         print(content.tag, n)
... 
horse 2
cowboy 2

Another option would be to select only children that have an "n" attribute 
using xpath:

>>> for content in pen.xpath("/corral/*[@n]"):
...     print(content.tag, content.get("n"))
... 
horse 2
cowboy 2





More information about the Python-list mailing list