testcases目標是使用lxmlappend 函式在節點內追加一個節點。
但是,編譯器回傳以下錯誤
AttributeError:“NoneType”物件沒有“附加”屬性
但是,將新節點追加到超級節點question中不會產生任何錯誤。但正如預期的那樣,這不會產生預期的輸出。
我可以知道如何解決這個問題嗎?
重現上述問題的代碼是
from lxml import etree
tree = etree.parse("d_xml.xml")
d='<testcase testtype="0" useasexample="0" hiderestiffail="0" mark="1.0000000" > <testcode> <text>print(solve(' \
'read_file("file_2.txt")))</text> </testcode><stdin><text></text></stdin><expected><text>status_2</text></expected' \
'><extra><text></text></extra><display><text>SHOW</text> </display></testcase>'
# contentnav = tree.find("question")
contentnav = tree.find("testcases")
contentnav.append(etree.XML(d))
print(etree.tostring(tree))
tree.write('output.xml', pretty_print=True, xml_declaration=True, encoding="utf-8")
d_xml.xml內容在下方或可通過鏈接下載
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<!-- question: 0 -->
<!-- question: 6675757 -->
<question type="coderunner">
<name>
<text>test_code_runner</text>
</name>
<questiontext format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">ex_test<br></p>]]></text>
</questiontext>
<generalfeedback format="html">
<text></text>
</generalfeedback>
<giveupallowed>0</giveupallowed>
<prototypeextra></prototypeextra>
<testcases>
<testcase testtype="0" useasexample="0" hiderestiffail="0" mark="1.0000000">
<testcode>
<text>print(solve(read_file('file_1.txt')))</text>
</testcode>
<stdin>
<text></text>
</stdin>
<expected>
<text>status_1</text>
</expected>
<extra>
<text></text>
</extra>
<display>
<text>SHOW</text>
</display>
</testcase>
<file name="fmlie.cpp" path="/" encoding="base64">aW1wb3J0IHJhbmRvbH</file>
<file name="idfile.txt" path="/" encoding="base64">TnVA2DQoyICg==</file>
</testcases>
</question>
</quiz>
預期輸出如下
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<!-- question: 0 -->
<!-- question: 6675757 -->
<question type="coderunner">
<name>
<text>test_code_runner</text>
</name>
<questiontext format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">ex_test<br></p>]]></text>
</questiontext>
<generalfeedback format="html">
<text></text>
</generalfeedback>
<giveupallowed>0</giveupallowed>
<prototypeextra></prototypeextra>
<testcases>
<testcase testtype="0" useasexample="0" hiderestiffail="0" mark="1.0000000">
<testcode>
<text>print(solve(read_file('file_1.txt')))</text>
</testcode>
<stdin>
<text></text>
</stdin>
<expected>
<text>status_1</text>
</expected>
<extra>
<text></text>
</extra>
<display>
<text>SHOW</text>
</display>
</testcase>
<testcases>
<testcase testtype="0" useasexample="0" hiderestiffail="0" mark="1.0000000">
<testcode>
<text>print(solve(read_file('file_2.txt')))</text>
</testcode>
<stdin>
<text></text>
</stdin>
<expected>
<text>status_2</text>
</expected>
<extra>
<text></text>
</extra>
<display>
<text>SHOW</text>
</display>
</testcase>
<file name="fmlie.cpp" path="/" encoding="base64">aW1wb3J0IHJhbmRvbH</file>
<file name="idfile.txt" path="/" encoding="base64">TnVA2DQoyICg==</file>
</testcases>
</question>
</quiz>
uj5u.com熱心網友回復:
find()方法查找背景關系元素的第一個子元素,在本例中為根元素。但testcases不是直接孩子,所以它回傳 None。
兩種解決方案:
找到question然后testcases擺脫它。
contentnav = doc.find("question").find("testcases")
使用 Xpath
contentnav = doc.xpath("//testcases")[0]
uj5u.com熱心網友回復:
你快到了;嘗試:
d=etree.fromstring('<testcase testtype="0" useasexample="0" hiderestiffail="0" mark="1.0000000" > <testcode> <text>print(solve(' \
'read_file("file_2.txt")))</text> </testcode><stdin><text></text></stdin><expected><text>status_2</text></expected' \
'><extra><text></text></extra><display><text>SHOW</text> </display></testcase>')
dest = tree.xpath('//question//testcases//testcase')[-1]
#this makes sure d is inserted as the last testcase
dest.addnext(d)
print(etree.tostring(tree).decode())
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/531625.html
標籤:xmllxml
