Hi, How can I extract my resulted list data to an xml file?
My resulted list is given below:
week=[{'item': Electrelane, 'weight': 140}, {'item': Kraftwerk, 'weight': 117},{'item': The Flaming Lips, 'weight': 113}]
Hi, How can I extract my resulted list data to an xml file?
My resulted list is given below:
week=[{'item': Electrelane, 'weight': 140}, {'item': Kraftwerk, 'weight': 117},{'item': The Flaming Lips, 'weight': 113}]
Since you don't provide any information on how you want to format your XML, i just invented my own notation.
week=[{'item': 'Electrelane', 'weight': 140}, {'item': 'Kraftwerk', 'weight': 117},{'item': 'The Flaming Lips', 'weight': 113}]
print "<?xml version='1.0' ?>"
print "<week>"
for day in week:
    print "  <day>"
    for key, value in day.items():
        print "    <%s>%s</%s>" % (key, value, key)
    print "  </day>"
print "</week>"
EDIT
To print to console, iterate over items in a similar way but change the output (by the print commands)
# enumerate the days in the week
for i, day in enumerate(week):
    print "day %d" % i
    # show values in sorted order
    for key in sorted(day):
        print "  - %s\t: %s" % (key, day[key])
Here's some code that uses xml.dom.minidom to build up the XML document.
week=[{'item': 'Electrelane', 'weight': 140}, {'item': 'Kraftwerk', 'weight': 117},{'item': 'The Flaming Lips', 'weight': 113}]
from xml.dom.minidom import getDOMImplementation
impl = getDOMImplementation()
document = impl.createDocument(None, "week", None)
week_element = document.documentElement
for entry in week:
    node = document.createElement("entry")
    for attr,value in entry.iteritems():
       node.setAttribute(attr,str(value))
    week_element.appendChild(node)
print document.toprettyxml()
Produces:
<?xml version="1.0" ?>
<week>
        <entry item="Electrelane" weight="140"/>
        <entry item="Kraftwerk" weight="117"/>
        <entry item="The Flaming Lips" weight="113"/>
</week>