相关疑难解决方法(0)

无法转储或写入ElementTree元素

我输出即使是最简单的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)

我必须做错事,但文档并没有指出任何明显的事情.

python xml elementtree

6
推荐指数
2
解决办法
8160
查看次数

如何使用Python复制xml元素?

我有一个看起来像这样的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)

python xml

5
推荐指数
1
解决办法
6493
查看次数

标签 统计

python ×2

xml ×2

elementtree ×1