Другой способ сделать это - преобразовать 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|