1

I m new to python. actually I m able to parse an xml but don't know how to write this into excel.

sample xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>
        <name>Strawberry Belgian Waffles</name>
        <price>$7.95</price>
        <description>Light Belgian waffles covered with strawberries and whipped cream</description>
        <calories>900</calories>
    </food>

my Code:

import xml.etree.ElementTree as etree
xmld = etree.parse('simple.xml')
root = xmld.getroot()
for child in root:
    for children in child:
     print children.tag," : % s" %children.text

output:

name  : Belgian Waffles
price  : $5.95
description  : Two of our famous Belgian Waffles with plenty of real maple syrup
calories  : 650
name  : Strawberry Belgian Waffles
price  : $7.95
description  : Light Belgian waffles covered with strawberries and whipped cream
calories  : 900

Need to write this into excel like

'tags' into Column A and 'values' into column B enter image description here

Tried with xlwt or openpyxl or pandas..but no luck...pls help..

3
  • Try to make a dictionary from your output, then make a dataframe from that and write it into an excel file. Commented Sep 7, 2016 at 22:01
  • Is this issue actual yet or you got the answer? Commented Sep 8, 2016 at 14:11
  • Hi Vlad...yep tried with openpyxl like: " for row in zip({children.tag},{children.text}): ws.append(row) wb.save("simple.xls") " Now i can get the output as i expected. Thanks a lot!!!! Commented Sep 8, 2016 at 18:22

2 Answers 2

1

From this site : http://www.python-excel.org/ , you can try :

Sign up to request clarification or add additional context in comments.

Comments

1

Thanks Vlad.. i have improved my code as per below:

import xml.etree.ElementTree as etree
from openpyxl import Workbook
xmld = etree.parse('simple.xml')
root = xmld.getroot()
wb = Workbook()
ws = wb.active
for child in root:
    for children in child:
     #print children.tag," : % s" %children.text
     for row in zip({children.tag},{children.text}):
        ws.append(row)
        wb.save("simple.xls")

Now i m successfully write into excel columns as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.