Skip to content
Advertisement

How to read specific data and write to csv file

I have data in xml file and I am reading 3 columns : price , name , calories

xml data

JavaScript

Code :

JavaScript

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

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.

Advertisement