我试图这样做
import xml.etree.ElementTree as ET
root = ET.Element('xml')
projects = ["a", "e", "u"]
for i in projects:
project = ET.SubElement(root,'project')
project.set('name', i)
designs = ["a", "e", "u"]
for u in designs:
design = ET.SubElement(project,'design')
design.set('name', u)
vars = ["a", "e", "u"]
for z in vars:
param = ET.SubElement(design,'param')
param.set('name', z)
value = ET.SubElement(param,'value')
value.text = "var"
tree = ET.ElementTree(root)
tree.write('projects.xml')
如果我用ipy.exe执行它,它的工作原理应该如此,但我需要从另一个程序的脚本接口执行它。脚本接口在.NET 2.0.50727.5466上使用IronPython 2.6.10920.0 它运行没有错误,但它创建的文件始终为空。如果我尝试删除所述文件,则说它仍然在程序中打开。是否有命令释放xml文件或我忽略了什么?
更新:如果我在运行脚本后关闭了有问题的程序,则会正常创建xml文件
更新:像这样的简单文件编写器可以很好地工作
f = open("pythontest.txt", "w")
projects = ["a", "e", "u"]
for i in projects:
f.write("Name: " + i)
f.close()
答案 0 :(得分:1)
也可以将文件对象传递给ElementTree构造函数。也许,尝试使用该文件创建文件,然后在最后关闭文件?