I have the below xml that i would want to convert to csv ( delimiter like '|' or octal ) using python. I tried converting the xml into dict and then to csv . I am looking if there are any easy or efficient methods to do so.
Problems with the below code :
- There is a tag xyz , which isn't needed in csv , so how to escape or ignore that? All i want is the data from tag abc.
- Would want the nested tag as prefix to underlying keys
- Expected output is a line with keys as header and the values below it.
XML sample :
<?xml version="1.0" encoding="utf-8"?>
<xyz date="2019-07-01T09:00:29">
<abc>
<id>23</id>
<uniqueid>23_0</uniqueid>
<Name></Name>
<Rate>
<mrp>6.40000</mrp>
<discount>10.00%</discount>
<discountmonths>2</discountmonths>
</Rate>
<fee>
<type>off</type>
<minimumfee>£1,500.75</minimumfee>
<maxfee>£10K</maxfee>
</fee>
</abc>
<abc>
<id>35</id>
<uniqueid>35_0</uniqueid>
<Name></Name>
<Rate>
<mrp>7.90000</mrp>
<discount>5.00%</discount>
<discountmonths>5</discountmonths>
</Rate>
<fee>
<type>offer</type>
<minimumfee>£1k</minimumfee>
<maxfee>£22,000</maxfee>
</fee>
</abc>
</xyz>
Code : ( don't mind the immaturity in the code, just a beginner , mostly a copy paste from multiple sites.. trying to understand if this works and then thought i will know where to start or modify the existing )
import xml.etree.ElementTree as ET
import xmltodict
import csv
tree = ET.parse('myxml_1.xml')
xml_data = tree.getroot()
xmlstr = ET.tostring(xml_data, encoding='utf-8', method='xml')
data_dict = dict(xmltodict.parse(xmlstr))
with open('test1.csv','w') as f:
w = csv.writer(f)
w.writerow(data_dict.keys())
w.writerow(data_dict.values())
output expected :
id|uniqueid|Name|rate_mrp|rate_discount|rate_discountmonths|fee_type|fee_minimumfee|fee_maxfee
23|23_0||6.40000|10.00%|2|off|£1,500.75|£10K
35|35_0||7.90000|5.00%|5|offer|£1k|£22,000