将数据合并到一个变量的xml数据

2024-06-10 16:01:45 发布

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

如果有多个许可证,则需要一个密钥。如果超过1个,则应将其归入一个许可证

输入文件数据:

<report>
    <report_header> SunDirect<report_header/>
        <Category> Single </Category>
        <Licenses>
            <License>1234</License>
            <License>525</License>
        </Licenses>
    <report_header> Tatasky<report_header/>
        <Category> Double </Category>
        <Licenses>
            <License>322</License>
            <License>1285</License>
            <License>1896</License>
        </Licenses>
    <report_header> SunDirect <report_header/>
        <Category> Multiple </Category>
        <Licenses>
            <License>1222</License>
        </Licenses>  
</report>

源代码:

import xml.etree.ElementTree as ET
tree = ET.parse('sample.xml')
root = tree.getroot()
way_list=[]
for item in root.findall('./reportheader'):
  for child in item:
    if child.tag == 'Category':
      way_list['category'] = child.text
    if child.tag == 'Licenses':
      for item1 in child.iter('License'):
        way_list['license'] = item1.text
        print(way_list)

电流输出:

{category: single, License: 1234}

{category: single, License: 525}

{category: Double, License: 322}

{category: Double, License: 1285}

{category: Double, License: 1896}

预期输出:

{category: single, License: 1234,525}

{category: Double, License: 322,1285,1896}

Tags: inreportchildforlicensexmlwaylist
1条回答
网友
1楼 · 发布于 2024-06-10 16:01:45

以下内容与您想要的内容非常接近,因此您可以尝试对其进行修改以满足您的确切需要:

way_list=[]
targets = tree.iterfind('*') #or in your case, possible 'root'
for m in targets: 
    d1 = dict()   
    d1[m.tag]=[]
    d1[m.tag]=m.text    
    licenses =[]
    for l in m.iterfind('*'):   
        licenses.append(l.text)
        d1[m.tag]=licenses
    way_list.append(d1)
chunk_size = len(tree.findall('report_header'))
seg = len(way_list)//chunk_size
for i in range(chunk_size):    
    print(way_list[i*seg:(i+1)*seg])

输出:

[{'report_header': ' SunDirect'}, {'Category': ' Single '}, {'Licenses': ['1234', '525']}]
[{'report_header': ' Tatasky'}, {'Category': ' Double '}, {'Licenses': ['322', '1285', '1896']}]
[{'report_header': ' SunDirect '}, {'Category': ' Multiple '}, {'Licenses': ['1222']}]

我仍然建议你们切换到像lxml或者其他支持xpath的东西

相关问题 更多 >