0

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

3
  • do you mean save the contents of the list in an xml file? Commented Dec 9, 2009 at 10:14
  • @BrianCore: Reading his comments on my answer, I now believe he wants to extract data from an xml file, which should result in the given list. Commented Dec 9, 2009 at 13:16
  • @BrainCore: You are right.I exactly want to save the contents of the list in an xml file.Do you people have Idea? @catchmeifyoutry:Sorry for asking vague question. Commented Dec 9, 2009 at 13:59

3 Answers 3

1

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])
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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??
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: stackoverflow.com/questions/1373707/xml-parsing-in-python
@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}]
1

You can trivially adjust this to your needs.

Comments

1

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>

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.