【发布时间】:2018-03-05 10:50:41
【问题描述】:
我有以下 XML 文件:
<xml_connection_file xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mcf.xsd">
<version> 1.0.0 </version>
<connection_group>
<id> 1 </id>
<connected_to>
<part>
<base> A </base>
</part>
<part>
<base> B </base>
</part>
</connected_to>
</connection_group>
</xml_connection_file>
我希望在主节点(xml_connection_file)中添加注释。我在 python3 中编写了以下代码,但出现错误说'lxml.etree._ElementTree' 没有属性'insert'。
from lxml import etree
from ansa import utils
my_selected_file = utils.SelectOpenFile(0, 'xml files (*.mcf)')
tree = etree.parse(my_selected_file[0])
comment = etree.Comment('my comments')
tree.insert(1,comment)
tree.write('new_file.mcf')
谁能告诉我我做错了什么?提前感谢!
【问题讨论】:
标签: python xml python-3.x xml-parsing python-3.4