Need a little parse help

Matt matthew_shomphe at countrywide.com
Tue May 10 16:17:41 EDT 2005


Alex Nordhus wrote:
> Im trying to grab a colum of data from a text file and write it to a
new
> file.
> I am having trouble getting It to write the data to newlines. Python
is
> making it one
> Long string without any spaces when it writes the file. The first
> character is capitalized in colum 2.
> I am trying to grab the 2nd colum in the file.
>
> import sys, string
>
> inputfilenames, outputfilename = sys.argv[1:-1], sys.argv[-1]
>
> for inputfilename in inputfilenames:
>
>     inputfile = open(inputfilename,'r')
>     outputfile = open(outputfilename, "w")
>     for ln in inputfile.readlines():
>         words = string.split(ln)
>         if len(words) >= 2:
> #	    print (words[1])
>   	    outputfile.write(words[1])
>
> When I use just print it prints out fine. Each on a dffrent line just
> the way I want it. But writing the file its all on one line. How do I
> get the data to goto new lines in the new file?
>
>
> Alex Nordhus
> Copy and Paste !
> http://www.pasteaway.com

Everyone else solved the "+'\n'" issue; but just as a nit, you should
use the ".split()" method on the string object ("ln").  In other words,
replace:

words = string.split(ln)
with
words = ln.split()




More information about the Python-list mailing list