I'm very new to python and please treat me as same. When i tried to convert the XML content into List of Dictionaries I'm getting output but not as expected and tried a lot playing around.
XML Content
<project>
<data><row><respondent>m0wxo5f6w42h3fot34m7s6xij</respondent><timestamp>10-06-16 11:30</timestamp><product>1</product><replica>1</replica><seqnr>1</seqnr><session>1</session><column><question>Q1</question><answer>a1</answer></column><column><question>Q2</question><answer>a2</answer></column></row>
<row><respondent>w42h3fot34m7s6x</respondent><timestamp>10-06-16 11:30</timestamp><product>1</product><replica>1</replica><seqnr>1</seqnr><session>1</session><column><question>Q3</question><answer>a3</answer></column><column><question>Q4</question><answer>a4</answer></column><column><question>Q5</question><answer>a5</answer></column></row>
</data>
</project>
Code i have used:
import xml.etree.ElementTree as ETtree = ET.parse(xml_file.xml) # import xml from
root = tree.getroot()
data_list = []for item in root.find('./data'): # find all projects nodedata = {} # dictionary to store content of each projectsfor child in item:data[child.tag] = child.text # add item to dictionary#-----------------for loop with subchild is not working as expcted in my casefor subchild in child:data[subchild.tag] = subchild.textdata_list.append(data)
print(data_list)headers = {k for d in data_list for k in d.keys()} # headers for csv
with open(csv_file,'w') as f:writer = csv.DictWriter(f, fieldnames = headers) # creating a DictWriter objectwriter.writeheader() # write headers to csvwriter.writerows(data_list)
Output for the data_list is getting the last info of question to the list of dictionaries. i guess the issue is at subchild forloop but im not understanding how to append the list with dictionaries.
[{'respondent': 'anonymous_m0wxo5f6w42h3fot34m7s6xij','timestamp': '10-06-16 11:30','product': '1','replica': '1','seqnr': '1','session': '1','column': '\n ,'question': 'Q2','answer': 'a2'
},
{
'respondent': 'w42h3fot34m7s6x','timestamp': '10-06-16 11:30','product': '1','replica': '1','seqnr': '1','session': '1','column': '\n ,'question': 'Q2','answer': 'a2'
}.......
]
I expect the below output, tried a lot but unable to loop over the column tag.
[{'respondent': 'anonymous_m0wxo5f6w42h3fot34m7s6xij','timestamp': '10-06-16 11:30','product': '1','replica': '1','seqnr': '1','session': '1','question': 'Q1','answer': 'a1'},{'respondent': 'anonymous_m0wxo5f6w42h3fot34m7s6xij','timestamp': '10-06-16 11:30','product': '1','replica': '1','seqnr': '1','session': '1','question': 'Q2','answer': 'a2'},{'respondent': 'w42h3fot34m7s6x','timestamp': '10-06-16 11:30','product': '1','replica': '1','seqnr': '1','session': '1','question': 'Q3','answer': 'a3'},{'respondent': 'w42h3fot34m7s6x','timestamp': '10-06-16 11:30','product': '1','replica': '1','seqnr': '1','session': '1','question': 'Q4','answer': 'a4'},{'respondent': 'w42h3fot34m7s6x','timestamp': '10-06-16 11:30','product': '1','replica': '1','seqnr': '1','session': '1','question': 'Q5','answer': 'a5'}
]
I have refereed so many stack overflow questions on xml tree but still didn't helped me.
any help/suggestion is appreciated.