<Root>
<SubRoot>
<Level1>A</Level1>
<Level2>B</Level2>
<Level3>C</Level3>
<Level4>D</Level4>
</SubRoot>
<SubRoot>
<Level1>1</Level1>
<Level2>2</Level2>
<Level3>3</Level3>
</SubRoot>
</Root>
for subroot in root.findall('SubRoot'):
level1 = subroot.find('Level1').text
level2 = subroot.find('Level2').text
level3 = subroot.find('Level3').text
level4 = subroot.find('Level4').text
print level1
print level2
print level3
print level4
import csv
import xml.etree.ElementTree as ET
xml_data = """<?xml version="1.0"?>
<Root>
...
</Root>"""
root = ET.fromstring(xml_data)
header = set()
rows = []
for subroot in root:
row = {}
for level in subroot:
header.add(level.tag)
row[level.tag] = level.text
rows.append(row)
with open('out.csv', 'w') as file:
writer = csv.DictWriter(file, fieldnames=sorted(header))
writer.writeheader()
writer.writerows(rows)
>>> import xml.etree.ElementTree as etree
>>> import csv
>>> import sys
>>>
>>> text = """\
... <Root>
... <SubRoot>
... <Level1>A</Level1>
... <Level2>B</Level2>
... <Level3>C</Level3>
... <Level4>D</Level4>
... </SubRoot>
...
... <SubRoot>
... <Level1>1</Level1>
... <Level2>2</Level2>
... <Level3>3</Level3>
... </SubRoot>
... </Root>
... """
>>>
>>> root = etree.fromstring(text)
>>>
>>> out = [[i.text for i in node] for node in root]
>>> out
[['A', 'B', 'C', 'D'], ['1', '2', '3']]
>>>
>>> out = [(i + [None] * (4 - len(i))) for i in out]
>>> out
[['A', 'B', 'C', 'D'], ['1', '2', '3', None]]
>>>
>>> writer = csv.writer(sys.stdout)
>>> writer.writerows(out)
A,B,C,D
1,2,3,
>>>