我有一个从xml文件中读取数据的python脚本
import xml.etree.ElementTree as ET
MAX_VALUE = 20
PATH_IN = "sweep.xml"
tree = ET.parse(PATH_IN)
root = tree.getroot()
projects = [""]*MAX_VALUE
designs = [""]*MAX_VALUE
params = [""]*MAX_VALUE
for i, project in enumerate(root):
projects[i] = project
print(project.get('name'))
for u, design in enumerate(project):
designs[u] = design
print(design.get('name'))
for x, param in enumerate(design):
params[x] = param
print(param.get('name'))
for x, info in enumerate(param):
print(info.text)
raw_input()
这是xml
<xml>
<project name="testProj">
<design name="des1">
<param name="mag_d">
<value>3mm</value>
<start>1</start>
<stop>5</stop>
<step>1</step>
</param>
<param name="mag_x">
<value>3mm</value>
<start>2</start>
<stop>7</stop>
<step>1</step>
</param>
<param name="mag_y">
<value>3mm</value>
<start>1</start>
<stop>2</stop>
<step>0.1</step>
</param>
</design>
<design name="des2">
<param name="mag_d">
<value>3mm</value>
<start>1</start>
<stop>5</stop>
<step>1</step>
</param>
<param name="mag_x">
<value>3mm</value>
<start>2</start>
<stop>7</stop>
<step>1</step>
</param>
<param name="mag_y">
<value>3mm</value>
<start>1</start>
<stop>2</stop>
<step>0.1</step>
</param>
</design>
</project>
</xml>
我现在要做的是将我正在阅读的信息添加到列表中。看起来应该是这样的:
Input=[['testProj','des1',[['mag_d',[1,2,3]],['mag_x',[2,3,4]]]], ['testProj','des2'[['mag_d',[1,2,3]],['mag_x',[2,3,4]]]]]
我真的不知道如何创建这样的列表。有人能指出我正确的方向吗?
答案 0 :(得分:2)
另一个没有构建列表的版本。 将xml.etree.ElementTree导入为ET
PATH_IN = "sweep.xml"
tree = ET.parse(PATH_IN)
def convert(root):
for p in root.iter('project'):
yield p.get('name')
for d in p.iter('design'):
yield d.get('name')
for e in d.iter('param'):
yield [e.attrib['name'], [p.text for p in e]]
it = convert(tree.getroot())
import pprint
pprint.pprint(list(it))
在OP输入上给出。
['testProj',
'des1',
['mag_d', ['3mm', '1', '5', '1']],
['mag_x', ['3mm', '2', '7', '1']],
['mag_y', ['3mm', '1', '2', '0.1']],
'des2',
['mag_d', ['3mm', '1', '5', '1']],
['mag_x', ['3mm', '2', '7', '1']],
['mag_y', ['3mm', '1', '2', '0.1']]]
答案 1 :(得分:1)
只需在每个级别创建一个列表,并将您读到的内容附加到其中。在每个级别的末尾,将列表附加到外级列表。
lst_lvl1 = [] # Top level list
for i, project in enumerate(root):
projects[i] = project
print(project.get('name'))
lst_lvl1.append(project.get('name')) # Append to the top level list
lst_lvl2 = [] # Create an empty 2nd level list
for u, design in enumerate(project):
designs[u] = design
print(design.get('name'))
lst_lvl2.append(design.get('name')) # Append to the 2nd level list
lst_lvl3 = [] # Create a 3rd level list...
for x, param in enumerate(design):
params[x] = param
print(param.get('name'))
lst_lvl3.append(param.get('name'))
lst_lvl4 = []
for x, info in enumerate(param):
print(info.text)
lst_lvl4.append(info.text)
lst_lvl3.append(lst_lvl4) # At the end of level 4, append what you got to the level-3 list
lst_lvl2.append(lst_lvl3) # At the end of level 3, append what you got to the level-2 list... etc
lst_lvl1.append(lst_lvl2)
print lst_lvl1