将xml数据转换为要加载到表中的字典列表

2024-04-26 14:15:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一段代码,将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岁

名字克里希纳

身份证11

美国广场

澳大利亚州

区域街1

州立大学

区域街2

州穆金

区域街3

名称ram

id 12

普莱斯英国酒店

国军

区域街4

州兰德

区域街5

国家甘地

区域街6

姓名chand

身份证13

伦敦广场

尼赫鲁州

区域街7

圣雄教

区域街8

州立公园

区域街9

预期产出:[{类型:'comp1',标题:'industry',年份:2019年,年龄:21岁,姓名:'krishna',id:11,地点:'usa',州:'aus',地区:'street1'},{类型:'comp1',标题:'industry',年份:2019年,年龄:21岁,姓名:'krishna',id:11,地点:'usa',州:'mus',地区:'street2},{类型:'comp1',标题:'industry',年份:2019年龄:21,名称:'krishna',id:11,地点:'usa'',州:'muskin',地区:'street3'},{类型:'comp1',头衔:'industry',年份:2019年,年龄:21岁,姓名:'ram',id:12,地点:'uk',州:'junej',地区:'street4'},{类型:'comp1',头衔:'industry',年份:2019年,年龄:21岁,姓名:'ram',id:12,地点:'uk',州:'rand',地区:'street5'},…]

我的主要目的是将数据加载到如下表中:

类型、头衔、年份、姓名、身份证、地点、州、地区

2019年工业公司,克里希纳,美国11号,澳大利亚1号街

2019年工业公司公司1号,克里希纳,11号,美国,密苏里州,街道2号

2019年工业公司公司1号,克里希纳,11号,美国,马斯金,街3号

2019年工业公司公司1号,英国12号拉姆,朱尼街4号

公司1,工业,2019年,拉姆,12,英国,兰德,街5号

2019年工业公司公司1号,英国12号拉姆,甘地,6号街

现在,我可以将数据转换成字典列表


Tags: id区域类型license公司areaprod地区
2条回答

仅使用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)

这里有一条路。请仔细阅读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)

相关问题 更多 >