2

我的代码

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

4

3 回答 3

3

看来您应该可以使用ET.tostring. 或者,您可以使用类的.write方法ElementTree

于 2012-11-28T00:24:39.227 回答
1
file_out=open('output.xml','wb',1000)
ElementTree(file_out).write(tree,encoding="ASCII")
file_out.close()
于 2012-11-28T02:52:07.527 回答
0

只是向您推荐lxml。你可以谷歌它或点击http://lxml.de

它非常易于使用且功能强大!该 API 与 ET 相当接近,如果不完全相同的话。

于 2012-11-28T04:16:18.493 回答