Emmanuel Vincent
asked on
Python - Converting XML to CSV
ab= [['name Belgian Waffles', 'price $5.95', 'description Two of our famous Belgian Waffles ', 'calories 650'] ]
I would like to parse this list to an CSV file in the table format
Ex. :
name price description Calories
Belgian Waffles $5.95 Two of our famous Belgian Waffles 650
Note : List size may vary . The values can vary . No hard coding should be there
I would like to parse this list to an CSV file in the table format
Ex. :
name price description Calories
Belgian Waffles $5.95 Two of our famous Belgian Waffles 650
Note : List size may vary . The values can vary . No hard coding should be there
Please, clarify your question. There is nothing about XML in the description, and the CSV file does not have any "table format".
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
the xml in question is
-<breakfast_menu>
-<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
+<food>
-<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
-<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
-<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
I managed to to extract this in to list in the following by using
def innerHtml(root):
text = ''
nodes = [ root ]
while not nodes==[]:
node = nodes.pop()
if node.nodeType==xml.dom.Nod e.TEXT_NOD E:
text += node.wholeText
else:
nodes.extend(node.childNod es)
return text
innerlist=[]
outerlist=[]
string2=[]
for statusNode in xmlFile.getElementsByTagNa me(xmlNode ):
for childNode in statusNode.childNodes:
if childNode.nodeType==xml.do m.Node.ELE MENT_NODE:
if innerHtml(childNode).strip () != '':
innerlist.append(childNode .nodeName+ " "+innerHtml(childNode).str ip())
outerlist.append(innerlist )
innerlist=[]
print (outerlist)
outerlist = [['name Belgian Waffles', 'price $5.95', 'description Two of our famous Belgian Waffles ', 'calories 650'] , ['name Berry-Berry Belgian Waffles','price $8.95','description Light Belgian waffles covered with an assortment ','calories 900']]
I want the list to be given xml to be converted into csv in the format
name price description Calories
Belgian Waffles $5.95 Two of our famous Belgian Waffles 650
Berry-Berry Belgian Waffles $8.95 Light Belgian waffles covered with an assortment 900
-<breakfast_menu>
-<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
+<food>
-<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
-<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
-<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
I managed to to extract this in to list in the following by using
def innerHtml(root):
text = ''
nodes = [ root ]
while not nodes==[]:
node = nodes.pop()
if node.nodeType==xml.dom.Nod
text += node.wholeText
else:
nodes.extend(node.childNod
return text
innerlist=[]
outerlist=[]
string2=[]
for statusNode in xmlFile.getElementsByTagNa
for childNode in statusNode.childNodes:
if childNode.nodeType==xml.do
if innerHtml(childNode).strip
innerlist.append(childNode
outerlist.append(innerlist
innerlist=[]
print (outerlist)
outerlist = [['name Belgian Waffles', 'price $5.95', 'description Two of our famous Belgian Waffles ', 'calories 650'] , ['name Berry-Berry Belgian Waffles','price $8.95','description Light Belgian waffles covered with an assortment ','calories 900']]
I want the list to be given xml to be converted into csv in the format
name price description Calories
Belgian Waffles $5.95 Two of our famous Belgian Waffles 650
Berry-Berry Belgian Waffles $8.95 Light Belgian waffles covered with an assortment 900
OK. You have managed to extract the data from XML... Now, there is the standard module named csv in Python. However, you do not want CSV output. The CSV means Comma Separated Values (one record = one line). You want formatted text file, right? Or what exact output do you need?
ASKER
No, i want the list extracted to be written in an csv file in the given format
name price description
Belgian Waffles $5.95 Two of our famous Belgian Waffles
Berry-Berry Belgian Waffles $8.95 Light Belgian waffles covered with an assortment
name price description
Belgian Waffles $5.95 Two of our famous Belgian Waffles
Berry-Berry Belgian Waffles $8.95 Light Belgian waffles covered with an assortment
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I have recommended this question be closed as follows:
Split:
-- pepr (https:#a42260791)
-- Shaun Vermaak (https:#a42258803)
If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
suhasbharadwaj
Experts-Exchange Cleanup Volunteer
I have recommended this question be closed as follows:
Split:
-- pepr (https:#a42260791)
-- Shaun Vermaak (https:#a42258803)
If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
suhasbharadwaj
Experts-Exchange Cleanup Volunteer