I'm trying to delete everything between if between is number 66:
I get the following error: TypeError: argument of type 'NoneType' is not iterable...if element.tag == 'answer' and '-66' in element.text:
What is wrong with that? Any help?
#!/usr/local/bin/python2.7
# -*- coding: UTF-8 -*-
from lxml import etree
planhtmlclear_utf=u"""
<questionaire>
<question>
<questiontext>What's up?</questiontext>
<answer></answer>
</question>
<question>
<questiontext>Cool?</questiontext>
<answer>-66</answer>
</question>
</questionaire>
"""
html = etree.fromstring(planhtmlclear_utf)
questions = html.xpath('/questionaire/question')
for question in questions:
for element in question.getchildren():
if element.tag == 'answer' and '-66' in element.text:
html.xpath('/questionaire')[0].remove(question)
print etree.tostring(html)
element.text seems to be None on some iterations. The error is saying that it cant look through None for "-66", so check that element.text is not None first like this:
The line its failing at in the xml is
<answer></answer>
where there is no text in between the tag.Edit (for the second part of your issue about combining tags):
You can use
BeautifulSoup
like this:Prints:
Here is a link where you can download the BeautifulSoup module.
Or, to do this a more compact way: