470,599 Members | 1,483 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,599 developers. It's quick & easy.

Need a little parse help


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

Jul 19 '05 #1
9 1275
write() doesn't automatically add a newline like print does.

You can either do:

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

or

print >> outputfile, words[1]

Jul 19 '05 #2
That worked! Thank you so much!

Jul 19 '05 #3
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()

Jul 19 '05 #4
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

--
Michael D. Hartl, Ph.D.
CTO, Quark Sports LLC
http://quarksports.com/

Jul 19 '05 #5
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
Sc***********@Acm.Org
Jul 19 '05 #6
"Michael Hartl" <mh****@post.harvard.edu> writes:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #7
Mike Meyer wrote:
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
Jul 19 '05 #8
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

--
Michael D. Hartl, Ph.D.
CTO, Quark Sports LLC
http://quarksports.com/

Jul 19 '05 #9
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

On Tuesday 10 May 2005 09:38 pm, Michael Hartl wrote:
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

--
Michael D. Hartl, Ph.D.
CTO, Quark Sports LLC
http://quarksports.com/


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

http://www.jamesstroud.com/
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by george young | last post: by
3 posts views Thread by Mike | last post: by
2 posts views Thread by MyNameIsnt | last post: by
25 posts views Thread by Jon Slaughter | last post: by
3 posts views Thread by Milagro | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.