gpt4 book ai didi

python - 条件 etree lxml 错误

转载 作者:行者123 更新时间:2023-12-01 06:06:38 25 4
gpt4 key购买 nike

我正在尝试删除之间的所有内容(如果其间的数字为 66):

我收到以下错误:TypeError:“NoneType”类型的参数不可迭代...如果 element.tag == 'answer' 和 element.text 中的“-66”:

这有什么问题吗?有什么帮助吗?

#!/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 在某些迭代中似乎是 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()

关于python - 条件 etree lxml 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7697097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com