Python: convert XML to CSV file

I have an XML file:

<hierachy> <att> <Order>1</Order> <attval>Data</attval> <children> <att> <Order>1</Order> <attval>Studyval</attval> </att> <att> <Order>2</Order> <attval>Site</attval> </att> </children> </att> <att> <Order>2</Order> <attval>Info</attval> <children> <att> <Order>1</Order> <attval>age</attval> </att> <att> <Order>2</Order> <attval>gender</attval> </att> </children> </att> </hierachy> 

I am trying to convert it to a CSV file as follows:

 Data,Studyval Date,Site Info,age Info,gender 

My problem is that both the parent and child names are the same - "att" and "attval". How can I tell Python to distinguish between both and give me a way out?

I tried this:

 import xml.etree.cElementTree as ET tree = ET.parse('input.xml') rebase = tree.getroot() list = [] for att in rebase.findall('att'): name = att.find('attval').text for each_att in att.findall('attval'): try: val = att.find('attval').text print name, val except AttributeError: print name 

and he printed the same things twice.

+5
source share
1 answer

Do not use the findall function, as it will look for att tags in the entire tree. Just move the tree in order from top to bottom and take the corresponding elements in them.

 from xml.etree import ElementTree tree = ElementTree.parse('input.xml') root = tree.getroot() for att in root: first = att.find('attval').text for subatt in att.find('children'): second = subatt.find('attval').text print('{},{}'.format(first, second)) 

What gives:

 $ python process.py Data,Studyval Data,Site Info,age Info,gender 
+5
source

All Articles