I have data in xml file and I am reading 3 columns : price , name , calories
xml data
JavaScript
x
39
39
1
<?xml version='1.0' encoding='utf-8'?>
2
<data>
3
<row>
4
<index>0</index>
5
<price>$5.95</price>
6
<name>Belgian Waffles</name>
7
<desc>Two of our famous Belgian Waffles with plenty of real maple syrup</desc>
8
<calories>650</calories>
9
</row>
10
<row>
11
<index>1</index>
12
<price>$7.95</price>
13
<name>Strawberry Belgian Waffles</name>
14
<desc>Light Belgian waffles covered with strawberries and whipped cream</desc>
15
<calories>900</calories>
16
</row>
17
<row>
18
<index>2</index>
19
<price>$8.95</price>
20
<name>Berry-Berry Belgian Waffles</name>
21
<desc>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</desc>
22
<calories>900</calories>
23
</row>
24
<row>
25
<index>3</index>
26
<price>$4.50</price>
27
<name>French Toast</name>
28
<desc>Thick slices made from our homemade sourdough bread</desc>
29
<calories>600</calories>
30
</row>
31
<row>
32
<index>4</index>
33
<price>$6.95</price>
34
<name>Homestyle Breakfast</name>
35
<desc>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</desc>
36
<calories>950</calories>
37
</row>
38
</data>
39
Code :
JavaScript
1
14
14
1
import xml.etree.ElementTree as ET
2
3
parse_xml = ET.parse('/content/sample_data/xyz.xml')
4
get_root_element = parse_xml.getroot()
5
6
7
for data in get_root_element.findall('row'):
8
9
prc = data.find('price')
10
nm = data.find('name')
11
cal = data.find('calories')
12
temp = prc.text + ',' + nm.text + ',' + cal.text
13
print(temp)
14
The above code gives me data but need to store this data to csv file
How do I need to write logic for this. Is it possible to do with pandas / csv
Need to add my headers as well to that csv file
Headers : price , name , calories
Advertisement
Answer
Solution of @kiric8494 is good enough, you can stay with it. You can also implement it using csv.DictWriter
which will be quite shorter:
JavaScript
1
10
10
1
import xml.etree.ElementTree as ET
2
from csv import DictWriter
3
4
parse_xml = ET.parse(r"/content/sample_data/xyz.xml")
5
root = parse_xml.getroot()
6
with open(r"/content/sample_data/abc.csv", "w", newline="") as f:
7
writer = DictWriter(f, fieldnames=("price", "name", "calories"), extrasaction="ignore")
8
writer.writeheader()
9
writer.writerows({e.tag: e.text for e in row} for row in root)
10
Basically we set up DictWriter
to ignore all fields except price
, name
and calories
and then pass generator to .writerows()
which construct dictionary of all child nodes of <row>
where key is tag and value is text.