I have a directory that contains multiple XML files, lets's say it contains the following 2:
<Record>
<RecordID>Madird01</RecordID>
<Location>Madird</Location>
<Date>07-09-2020</Date>
<Time>07u43m55s</Time>
<Version>2.0.1</Version>
<Version_2>v1.9</Version_2>
<Max_30e>
<I_25Hz_1s>56.40</I_25Hz_1s>
<I_25Hz_2s>7.44</I_25Hz_2s>
</Max_30e>
<Max_30e>
<I_75Hz_1s>1.56</I_75Hz_1s>
<I_75Hz_2s>0.36</I_75Hz_2s>
</Max_30e>
</Record>
And:
<Record>
<RecordID>London01</RecordID>
<Location>London</Location>
<Date>07-09-2020</Date>
<Time>08u53m45s</Time>
<Version>2.0.1</Version>
<Version_2>v1.9</Version_2>
<Max_30e>
<I_25Hz_1s>56.40</I_25Hz_1s>
<I_25Hz_2s>7.44</I_25Hz_2s>
</Max_30e>
<Max_30e>
<I_75Hz_1s>1.56</I_75Hz_1s>
<I_75Hz_2s>0.36</I_75Hz_2s>
</Max_30e>
</Record>
Now I want to convert this to an excel file that shows every XML file in horizontal order like this:
I tried to convert the XML to CSV string first and then to Excel but I got stuck, there should be easier ways.
This is my current code:
import xml.etree.ElementTree as ET
import os
xml_root = r'c:\data\Desktop\Blue\XML-files'
for file in os.listdir(xml_root):
xml_file_path = os.path.join(xml_root, file)
tree = ET.parse(xml_file_path)
root = tree.getroot()
tree = ET.ElementTree(root)
for child in root:
mainlevel = child.tag
xmltocsv = ''
for elem in root.iter():
if elem.tag == root.tag:
continue
if elem.tag == mainlevel:
xmltocsv = xmltocsv + '\n'
xmltocsv = xmltocsv + str(elem.tag).rstrip() + str(elem.attrib).strip() + ';' + str(elem.text).rstrip() + ';