zooted
zooted

Reputation: 165

XML write instead of print

So I want to write to a file instead of printing to the screen. I'm having trouble with doing that with python 2.7.

Here is my script:

from xml.dom import minidom
import sys
import os

xmldoc = minidom.parse('c:/Python27/file.xml')
itemlist = xmldoc.getElementsByTagName('User')

for s in itemlist:
    print (s.attributes['name'].value)

This actually prints out perfectly I just need it to print to a file. I'm sort of running into some issues.

Upvotes: 0

Views: 51

Answers (1)

Robbert
Robbert

Reputation: 6592

I'm no python expert but you should be able to open the source file and then write the string to it

f = open(filename, 'w')
for s in itemlist:
   f.write(s.attributes['name'].value + "\n")

Upvotes: 2

Related Questions