我创建了一个 XML-Reader,它创建了一个非常愚蠢的格式化列表,但要使用它的脚本需要这种方式。
import xml.etree.ElementTree as ET
PATH_IN = "<Path>\sweep.xml"
tree = ET.parse(PATH_IN)
root = tree.getroot()
Input = []
for project in root:
for design in project:
list_lvl2 = []
list_lvl2.append(project.get('name'))
list_lvl2.append(design.get('name'))
list_lvl2.append('')
list_lvl3 = []
for param in design:
list_lvl4 = []
list_lvl4.append(param.get('name'))
list_lvl5 = []
for steps in param:
list_lvl5.append(steps.text)
list_lvl4.append(list_lvl5)
list_lvl4.append(param.get('unit'))
list_lvl3.append(list_lvl4)
list_lvl2.append(list_lvl3)
Input.append(list_lvl2)
问题是我必须在.NET 2.0.50727.5466 上使用 IronPython 2.6.10920.0 的程序的脚本接口中执行它。它会引发以下错误:
*Global - Messages
[error] ImportException: No module named expat; use SimpleXMLTreeBuilder instead In file "<Path>/sweep.py", line 36 ---- While executing script: <Path>/sweep.py
[error] Error executing script in <Path>\sweep.py:
[error] ImportException: No module named expat; use SimpleXMLTreeBuilder instead In file "<Path>/sweep.py", line 36 ---- While executing script: <Path>/sweep.py
[error] Error executing script in <Path>\sweep.py:
我不明白的是,这个使用相同模块的脚本工作正常:
import xml.etree.ElementTree as ET
path = "<Path>\projects.xml"
root = ET.Element('xml')
projects = oDesktop.GetProjectList()
for i in projects:
project = ET.SubElement(root,'project')
project.set('name', i)
designs = oDesktop.SetActiveProject(i).GetTopDesignList()
for u in designs:
design = ET.SubElement(project,'design')
design.set('name', u)
vars = oDesktop.SetActiveProject(i).GetDesign(u).GetVariables()
for z in vars:
param = ET.SubElement(design,'param')
param.set('name', z)
value = ET.SubElement(param,'value')
value.text = oDesktop.SetActiveProject(i).GetDesign(u).GetVariableValue(z)
f = open(path, "w")
tree = ET.ElementTree(root)
tree.write(f)
f.close()
oDesktop 等只是获取数据的程序特定功能。我真的不明白为什么第一个脚本不起作用,我猜是因为 NET 2.0。
更新:在查看了 expat 是什么之后,我更加困惑,因为我从不在任何地方使用它,而且我使用的所有 elementtree 函数都应该在这个版本的 Iron Python 中得到支持。仍然给我这一行的外籍人士错误tree = ET.parse(PATH_IN)
更新:我尝试使用from elementtree import SimpleXMLTreeBuilder as ET
导致:
*Global - Messages
[error] ImportException: No module named elementtree In file "<Path>/sweep.py", line 3 ---- While executing script: <Path>/sweep.py
[error] Error executing script in <Path>\sweep.py: