Need a little parse help

A

Alex Nordhus

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
 
L

Lonnie Princehouse

write() doesn't automatically add a newline like print does.

You can either do:

outputfile.write(words[1] + '\n')

or

print >> outputfile, words[1]
 
M

Matt

Alex said:
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()
 
M

Michael Hartl

I'd also like to note that both the inputfiles variable and the
readlines() method are superfluous; to iterate through the file line by
line, use either

for line in open(inputfilename):
# do something with line

or (my personal preference, since I like to think of the file as a
thing rather than an action)

for line in file(inputfilename):
# do something with line

(Note also that the 'r' argument to open/file is the default and so can
be omitted.)

Michael
 
S

Scott David Daniels

Alex Nordhus wrote:
....
for ln in inputfile.readlines():
words = string.split(ln)
if len(words) >= 2:
# print (words[1])
Try:
print >>outputfile, words[1]

--Scott David Daniels
(e-mail address removed)
 
M

Mike Meyer

Michael Hartl said:
I'd also like to note that both the inputfiles variable and the
readlines() method are superfluous; to iterate through the file line by
line, use either

for line in open(inputfilename):
# do something with line

or (my personal preference, since I like to think of the file as a
thing rather than an action)

for line in file(inputfilename):
# do something with line

I'd like to note that failing to close the file explicitly is a bad
habit. You really should invoke the close method, rather than relying
on the garbage collector to close them for you. This means you do need
a variable to hold the file object. 99% of the time nothing bad will
happen if you skip this, but that 1% can always come back to byte you
in the *ss.

<mike
 
P

Peter Hansen

Mike said:
I'd like to note that failing to close the file explicitly is a bad
habit. You really should invoke the close method, rather than relying
on the garbage collector to close them for you. This means you do need
a variable to hold the file object. 99% of the time nothing bad will
happen if you skip this, but that 1% can always come back to byte you
in the *ss.

While that's generally true, it's worth noting that for small utility
scripts of the "throw-away" variety, adding in the code necessary to do
properly what Mike describes can make the difference between the code
being quick and simple and (at least for those fairly new to Python)
awkward and confusing, and thus potentially buggy.

In my opinion, if the code fits on one screen and just reads stuff from
one file and, maybe, writes to another, you can safely and with clean
conscience ignore Mike's advice (but remember it for later!).

-Peter
 
M

Michael Hartl

Mike mentions an important point, and I've been bitten by the
phenomenon Mike mentions---but only when *writing* to files. They
should always be closed explicitly, as in

f = file(filename, 'w')
f.write(somestring)
f.close()

On the other hand, I've never encountered a problem with the "for line
in file(filename)" idiom. A similar time-saver is writing something
like

s = file(filename).read()

which puts the entire file as a string in the variable s. In this
case, as in the file iteration case, we save two lines of code and one
variable. It may not sound like much, but

f = file(filename)
s = f.read()
f.close()

seems much more cumbersome to me, especially when doing a lot of file
reads.

In short, my experience is that explicitly closing a file is
unnecessary when reading. I could be wrong, though, and I'd be very
interested to see an example of either idiom above leading to problems.

Michael
 
J

James Stroud

My understanding is that Python code should keep as many possible
implementations in mind. For example, I have been told that it would be
unwise to do something like this in Jython because the Java GC will not
reclaim the file resources:

for afile in more_than_just_a_few_files:
for aline in open(afile):
doit(aline)
# unwisely forget to close afile

I could be wrong, though.

James

Mike mentions an important point, and I've been bitten by the
phenomenon Mike mentions---but only when *writing* to files. They
should always be closed explicitly, as in

f = file(filename, 'w')
f.write(somestring)
f.close()

On the other hand, I've never encountered a problem with the "for line
in file(filename)" idiom. A similar time-saver is writing something
like

s = file(filename).read()

which puts the entire file as a string in the variable s. In this
case, as in the file iteration case, we save two lines of code and one
variable. It may not sound like much, but

f = file(filename)
s = f.read()
f.close()

seems much more cumbersome to me, especially when doing a lot of file
reads.

In short, my experience is that explicitly closing a file is
unnecessary when reading. I could be wrong, though, and I'd be very
interested to see an example of either idiom above leading to problems.

Michael

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top