tags:

views:

117

answers:

3

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}]

+1  A: 

You can trivially adjust this to your needs.

badp
+1  A: 

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])
catchmeifyoutry
Hi, Thanks a lot,I got it.I have another question.If I want to get this week values from that xml file to console to show formatted output(means only to show values in nice way) as well then I how can I do?
rahman.bd
Its ok.But what I exactly wanted is like to have separate xml file which will have output data(week data in element tree format).And from that xml file I want to show output in console.Any Idea please??
rahman.bd
You should edit and clarify your original question so that more people can read it and understand what you want. As I understand now you want to parse an XML file, and then output the results to console. This is not what your current question states at all.Or, you can search for previous questions regarding python XML parsing, such as: http://stackoverflow.com/questions/1373707/xml-parsing-in-python
catchmeifyoutry
Another question? http://stackoverflow.com/questions/ask
badp
@catchmeifyoutry: clarifying on my previous question , I want an external xml file like .xml extention which will have the output from my list contents.Not really xml parsing.For example my list :week=[{'item': Electrelane, 'weight': 140}, {'item': Kraftwerk, 'weight': 117},{'item': The Flaming Lips, 'weight': 113}]
rahman.bd
+1  A: 

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>
Dave Webb
@Dave:Thanks for ur ans.
rahman.bd