如果有多个许可证,则需要一个密钥。如果超过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}
以下内容与您想要的内容非常接近,因此您可以尝试对其进行修改以满足您的确切需要:
输出:
我仍然建议你们切换到像lxml或者其他支持xpath的东西
相关问题 更多 >
编程相关推荐