0

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.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
zooted
  • 165
  • 1
  • 1
  • 12

1 Answers1

2

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")
Robbert
  • 6,481
  • 5
  • 35
  • 61
  • Sweet, now do you know how I would write that vertically instead of horizontal? – zooted Dec 22 '15 at 17:44
  • The new line character at the end should take care of that. If your'e running on windows and opening the file with Notepad, you probably need to use `"\r\n"` instead of `"\n"`. http://stackoverflow.com/questions/15433188/r-n-r-n-what-is-the-difference-between-them – Robbert Dec 22 '15 at 17:47
  • Are you seeing something like `Attribute1Attribute2Attribute3`? – Robbert Dec 22 '15 at 18:12