我输出即使是最简单的Element(Tree)实例也有问题.如果我在Python 2.7.1中尝试以下代码
>>> from xml.etree.ElementTree import Element, SubElement, tostring
>>> root = Element('parent')
>>> child = Element('child')
>>> SubElement(root, child)
>>> tostring(root)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
TypeError: cannot serialize <Element 'root' at 0x9a7c7ec> (type Element)
Run Code Online (Sandbox Code Playgroud)
我必须做错事,但文档并没有指出任何明显的事情.
我有一个看起来像这样的XML文件:
<a>
<b>
<c>World</c>
</b>
</a>
Run Code Online (Sandbox Code Playgroud)
并应如下所示:
<a>
<b>
<c>World</c>
<c>World</c>
</b>
</a>
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
import xml.etree.ElementTree as ET
file=open("6x6.xml", "r")
site=file.ET.Element("b")
for c in file:
site.append(c)
file.write("out.xml")
file.close()
Run Code Online (Sandbox Code Playgroud)