for place in root.findall('Place'):
row = {}
for identity in place.findall('Identity'):
for placeid in identity.findall('PlaceId'):
header.add(placeid.tag)
row[placeid.tag] = placeid.text
for locationlist in place.findall('LocationList'):
for location in locationlist.findall('Location'):
for address in location.findall('Address'):
for parsed in address.findall('Parsed'):
for countrycode in parsed.findall('CountryCode'):
header.add(countrycode.tag)
row[countrycode.tag] = countrycode.text
places_data.append(row)
>>> 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,
>>>
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)