Ошибки с условным этри lxml - PullRequest
1 голос
/ 08 октября 2011

Я пытаюсь удалить все между, если между это число 66:

Я получаю следующую ошибку: TypeError: аргумент типа 'NoneType' не повторяется ... если element.tag == 'answer' и '-66' в element.text:

Что с этим не так? Любая помощь?

#!/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) 

Ответы [ 2 ]

1 голос
/ 08 октября 2011

Альтернативой проверки, если element.text равно None, является уточнение вашего XPath:

questions = html.xpath('/questionaire/question[answer/text()="-66"]')
for question in questions:
    question.getparent().remove(question)

Скобки [...] означают «такой, что». Итак

question                          # find all question elements
[                                 # such that 
  answer                          # it has an answer subelement
    /text()                       # whose text 
  =                               # equals
  "-66"                           # "-66"
]
1 голос
/ 08 октября 2011

element.text кажется None на некоторых итерациях.Ошибка говорит о том, что он не может просмотреть None для «-66», поэтому убедитесь, что element.text не является None первым, например так:

html = etree.fromstring(planhtmlclear_utf)
questions = html.xpath('/questionaire/question')
for question in questions:
    for element in question.getchildren():   
        if element.tag == 'answer' and element.text and '-66' in element.text:
            html.xpath('/questionaire')[0].remove(question)
print etree.tostring(html) 

Строка, в которой происходит сбой в xml, - <answer></answer>там, где между тегом нет текста.


Редактировать ( для второй части вашего вопроса о комбинировании тегов) :

Вы можете использовать BeautifulSoup следующим образом:

from lxml import etree
import BeautifulSoup

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 element.text and '-66' in element.text:
            html.xpath('/questionaire')[0].remove(question)

soup = BeautifulSoup.BeautifulStoneSoup(etree.tostring(html))
print soup.prettify()

Отпечатки:

<questionaire>
 <question>
  <questiontext>
   What's up?
  </questiontext>
  <answer>
  </answer>
 </question>
</questionaire>

Вот ссылка, по которой вы можете скачать BeautifulSoup module .


Или, чтобы сделать это более компактным способом:

from lxml import etree
import BeautifulSoup    

# abbreviating to reduce answer length...
planhtmlclear_utf=u"<questionaire>.........</questionaire>"

html = etree.fromstring(planhtmlclear_utf)
[question.getparent().remove(question) for question in html.xpath('/questionaire/question[answer/text()="-66"]')]
print BeautifulSoup.BeautifulStoneSoup(etree.tostring(html)).prettify()
...