Link to home
Start Free TrialLog in
Avatar of Emmanuel Vincent
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
Avatar of pepr
pepr

Please, clarify your question. There is nothing about XML in the description, and the CSV file does not have any "table format".
SOLUTION
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Emmanuel Vincent

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.Node.TEXT_NODE:
            text += node.wholeText
        else:            
            nodes.extend(node.childNodes)          
    return text
innerlist=[]
outerlist=[]
string2=[]


for statusNode in xmlFile.getElementsByTagName(xmlNode):
    for childNode in statusNode.childNodes:
        if childNode.nodeType==xml.dom.Node.ELEMENT_NODE:
            if innerHtml(childNode).strip() != '':
                    innerlist.append(childNode.nodeName+"  "+innerHtml(childNode).strip())
    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?
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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