我已经编写了将xml数据转换成字典列表并加载到表中的代码。
输入文件数据:
<report>
<report_header type='comp1' title='industry' year='2019' />
<report_body age='21'>
<Prod name='krishna' id='11' place='usa'>
<License state='aus' area= 'street1'>
</License>
<License state='mus' area= 'street2'>
</License>
<License state='mukin' area= 'street3'>
</License>
</Prod>
<Prod name='ram' id='12' place='uk'>
<License state='junej' area= 'street4'>
</License>
<License state='rand' area= 'street5'>
</License>
<License state='gandhi' area= 'street6'>
</License>
</Prod>
<Prod name='chand' id='13' place='london'>
<License state='nehru' area= 'street7'>
</License>
<License state='mahatma' area= 'street8'>
</License>
<License state='park' area= 'street9'>
</License>
</Prod>
</report_body>
</report>
代码:
import xml.etree.ElementTree as ET
tree = ET.parse('sample.xml')
root = tree.getroot()
way_list=[]
for item in root.iter():
way_list.append(dict(item.attrib))
for k,v in [(k,v) for x in way_list for (k,v) in x.items()]:
print(k,v)
输出: 输入comp1
标题行业
2019年
21岁
名字奎师那
id 11
放置美国
状态aus
区域街道1
状态mus
区域街道2
状态mukin
区域街道3
名称ram
id 12
放置英国
国家六月
区域街道4
国家兰特
区域街道5
甘地州
区域街道6
名字chand
id 13
伦敦广场
状态nehru
区域街道7
圣雄状态
区域街道8
国家公园
地区街道9
预期产量: [{类型:'comp1',标题:'industry',年份:2019,年龄:21,名称:'krishna',id:11,地点:'usa' ,state:'aus',area:'street1'},{type:'comp1',title:'industry',年份:2019,年龄:21,name:'krishna',id:11,place:'usa' ,state:'mus',area:'street2'},{type:'comp1',title:'industry',年份:2019,年龄:21,name:'krishna',id:11,place:'usa' ,state:'muskin',area:'street3'},{type:'comp1',title:'industry',年份:2019,年龄:21,name:'ram',id:12,place:'uk' ,state:'junej',area:'street4'},{type:'comp1',title:'industry',年份:2019,年龄:21,name:'ram',id:12,place:'uk' ,state:'rand',area:'street5'},.........等]
我的主要目的是将数据加载到如下表中:
类型,标题,年份,名称,id,位置,州,地区
comp1,industry,2019,克里希纳,11,美国,澳大利亚,街道1
comp1,industry,2019,krishna,11,usa,mus,street2
comp1,industry,2019,krishna,11,usa,muskin,street3
comp1,industry,2019,ram,12,uk,junej,street4
comp1,industry,2019,ram,12,uk,rand,street5
comp1,industry,2019,ram,12,uk,gandhi,street6
现在,我无法将数据转换为词典列表。