I'm being provided an XML file containing the data I need and I need to convert it to a CSV.
This should be straightforward, but the number of children for the "repeating unit" of the XML is not always the same.
What I am trying to work out is how best to iterate the the childen of each child element, until there are no more, and return this as one "line". The end output should be a list of dictionaries (one list per "row" for the CSV).
As an example
<repeatingunit>
<city>
<name>London</name>
</city>
<station>
<name>Southwark</name>
<tubeline>
<name>Jubilee</name>
</tubeline>
</repeatingunit>
<repeatingunit>
<city>
<name>London</name>
<county>UK</county>
<station>
<name>Mile End</name>
</station>
</repeatingunit>
This should result in:
{'city|name':'London','station|name':'Southwark','station|tubeline|name': 'Jubilee'},{'city|name':'London','city|country':'UK','station|name':'Mile End'}
I had been using xml.etree.ElementTree and root.iter, I'm happy with the looping but its the dynamism.
I tried to use the logic for multiple nested list here but to no avail. Can someone point me in the right direction suggest a new approach?
I'm aware the dictionaries being different length at the end is not ideal for writing out to csv but I can handle that based on my desired output.