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 9 1353
write() doesn't automatically add a newline like print does.
You can either do:
outputfile.write(words[1] + '\n')
or
print >> outputfile, words[1]
That worked! Thank you so much!
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()
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/
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
"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.
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
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/
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: george young |
last post by:
For each run of my app, I have a known set of (<100) wafer names.
Names are sometimes simply integers, sometimes a short string, and
sometimes a short string followed by an integer, e.g.:
5, 6,...
|
by: Mike |
last post by:
Hey guys I am pulling my hair out on this problem!!!!! Any help or ideas or
comments on how to make this work I would be grateful! I have been working
on this for the past 4 days and nothing I do...
|
by: MyNameIsnt |
last post by:
Can anyone tell me why, when I click on the buttons it register 2
characters on the display?
if you use the right mousebutton it works ok, but the buttons dont
flash??
it works fine without the...
|
by: Mika M |
last post by:
Hi!
I have some problems with setting PrintDocument margins using
PageSetupDialog. Here some code to explain my problem...
First PrintDocument declaring this way...
Private pd As...
|
by: raypjr |
last post by:
Hi everyone. I'm new here and hope I can get a little advice on how to list my array into a ListBox. I have my structure and array of structures. I need help with a For Loop that will list the...
| |
by: raypjr |
last post by:
Hi everyone. I'm new here and hope I can get a little advice on how to list my array into a ListBox. I have my structure and array of structures. I need help with a For Loop that will list the...
|
by: Jon Slaughter |
last post by:
I have some code that loads up some php/html files and does a few things to
them and ultimately returns an html file with some php code in it. I then
pass that file onto the user by using echo. Of...
|
by: Milagro |
last post by:
Hello Everyone,
I'm trying to debug someone elses php code. I'm actually a Perl
programmer, with OO experience, but not in php.
The code is supposed to upload a photo from a form and save it...
|
by: jenkinsloveschicken |
last post by:
I am needing help passing a GET variable to a receiving php page. This html page does not contain an forms for submission(POST/GET methods).
First I need to parse the URL string using...
|
by: jamwil |
last post by:
What up friends, I'm using this little function to parse hyperlinks in plain text and convert them into links. It goes like soprivate function hyperlink($text) {
// match protocol://address/path/...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |