用数组将XML解析为文件

2024-06-16 09:42:33 发布

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

我需要生成下面的文件。使用python解析XML示例:

示例XML

<fruits>
<tag beginTime="20181125020000" endTime="20181202020000">
<EventId>16778</EventId>
    <item color="red">
        <name>apple</name>
        <count>1</count>
        <subtag>
            <Info name="Eid">396</Info>
            <Info name="New">397</Info>
        </subtag>
    </item>
    <item color="yellow">
        <name>banana</name>
        <count>2</count>
        <subtag>
            <Info name="Eid">500</Info>
            <Info name="New">650</Info>
            <Info name="Col">999</Info>
        </subtag>
    </item>
</tag>  

期望输出:

20181125020000|20181202020000|16778|red|apple|1|Eid;396;New;397|
20181125020000|20181202020000|16778|yelow|banana|1|Eid;500;New;650;Col;999|

Tags: nameinfo示例applenewtagcountred
2条回答

试试这个代码。你知道吗

import xml.etree.ElementTree as Et

file = Et.parse('some.xml')

tags = file.findall('tag')
for tag in tags:
temp1 = []
beginTime = tag.get('beginTime')
temp1.append(beginTime)
endTime = tag.get('endTime')
temp1.append(endTime)
eventId = tag.find('EventId').text
temp1.append(eventId)
items = tag.findall('item')

for item in items:
    temp2 = []
    color = item.get('color')
    temp2.append(color)
    name = item.find('name').text
    temp2.append(name)
    count = item.find('count').text
    temp2.count(count)
    infos = item.find('subtag').findall('Info')

    temp3 = []
    for info in infos:
        name = info.get('name')
        value = info.text
        temp3.append(name)
        temp3.append(value)
    temp3 = [';'.join(temp3)]
    result = temp1 + temp2 + temp3
    result = '|'.join(result)
    print(result)

另一种方法是将XML转换为json

import xmltodict

with open('file.xml') as f:
    d = xmltodict.parse(f.read())['fruits']['tag']

for i in d['item']:
    subtag = []
    for s in i['subtag']['Info']:
        subtag.append('{};{}'.format(s['@name'], s['#text']))
    print('{}|{}|{}|{}|{}|{}|{}|'.format(d['@beginTime'], d['@endTime'], d['EventId'], i['@color'], i['name'], i['count'], ';'.join(subtag)))

输出:

20181125020000|20181202020000|16778|red|apple|1|Eid;396;New;397|
20181125020000|20181202020000|16778|yellow|banana|2|Eid;500;New;650;Col;999|

相关问题 更多 >