lxml ignore none in getchildren
Sayth Renshaw
flebber.crue at gmail.com
Tue Oct 4 07:59:07 EDT 2016
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
More information about the Python-list
mailing list