将xml数据转换成字典列表以加载到表中

我已经编写了将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

现在,我无法将数据转换为词典列表。

liupp41 回答:将xml数据转换成字典列表以加载到表中

这是一种方式。阅读csv module

import csv,os,sys,io
from xml.etree import ElementTree

data = """\
<report>
<report_header type='comp1'  title='industry' year='2019' />
    <report_body>
    <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>
"""

fieldnames = ['type','title','year','name','id','place','state','area']
writer = csv.DictWriter(sys.stdout,fieldnames=fieldnames)
writer.writeheader()
tree = ElementTree.parse(io.StringIO(data))
report_header = tree.find('report_header')
report_body = tree.find('report_body')
for Prod in report_body.findall('Prod'):
    for License in Prod.findall('License'):
        d = {}
        d.update(License.attrib)
        d.update(Prod.attrib)
        d.update(report_header.attrib)
        writer.writerow(d)
,

仅使用ElementTree。

import xml.etree.ElementTree as ET

tree = ET.parse('sample.xml')
root = tree.getroot()
dict_rep= root.find('report_header').attrib
dict_rep.update(root.find('report_body').attrib)
way_list=[]
for prod in root.iter('Prod'):
  dict_line = dict_rep
  dict_line.update(prod.attrib)
  for lic in prod.iter('License'):
    dict_line.update(lic.attrib)
    print(dict_line)
    way_list.append(dict_line)
本文链接:https://www.f2er.com/3126644.html

大家都在问