我的代码
tree = ET.Element('e')
tree.attrib['u'] = 'F' + str(srh) + '_0'
if procedural_variables:
pvs = ET.SubElement(tree, 'pvs')
for procedural_variable in procedural_variables:
pv = ET.SubElement(pvs, 'pv')
n = ET.SubElement(pv, 'n')
n.text = procedural_variable['name']
v = ET.SubElement(pv, 'v')
v.text = str(procedural_variable['value'])
ET.dump(tree)如何将此tree编写为某个file.xml
发布于 2012-11-28 08:24:39
看起来你应该能够使用ET.tostring了。或者,您可以使用ElementTree类的.write method
发布于 2012-11-28 10:52:08
file_out=open('output.xml','wb',1000)
ElementTree(file_out).write(tree,encoding="ASCII")
file_out.close()发布于 2012-11-28 12:16:18
只需向您推荐lxml即可。你可以在谷歌上搜索,或者点击http://lxml.de。
它非常容易使用并且健壮!该API与ET的API非常接近,如果不相同的话。
https://stackoverflow.com/questions/13595674
复制相似问题