[Tutor] Re: Readlines code.
Rick Pasotto
rick@niof.net
Tue Jul 1 22:17:01 2003
On Tue, Jul 01, 2003 at 09:45:58PM -0400, Cliff Martin wrote:
> Rick,
>
> Thanks for the help. I understand what you did and this wraps up the
> file for printing but once the stuff is printed it no longer exists in
> the assignment words. I want to manipulate and calculate (once I've
> converted the strings into numbers) and I need this split and extracted
> \n file to exist. I can't use a standard function because once the
> return statement is executed the data is written to file. What am I
> still not understanding.
Are you sure you can't do everything you need to do to each line as you
encounter it? Why do you need to process the entire set of lines a
second time?
If you really need to do multiple passes through the file then perhaps
the best thing would be:
myfile = file("c:/transfer/filename") # 'file' is the new 'open'
Now 'myfile' is a list of all the lines in the disk file which you can
go through as many times as you want.
for i in range(len(myfile)):
myfile[i] = myfile[i].strip().split()
for i in range(len(myfile)):
# do something else to every line
I really suspect that if you re-think what you're doing you'll find that
you can do everything in one pass.
[And please don't put your reply at the top of the message. Replies
logically come *after* what they are replying to, not before.]
> On Mon, Jun 30, 2003 at 09:43:37PM -0400, Cliff Martin wrote:
>
> >>Hi,
> >>
> >>I'm trying to read in an ASCII file of a fairly large set of data with
> >>data separated by spaces and each line ending with a linefeed. If I use
> >>readlines() I get all the data but each line still has a \n tacked on.
> >>If I use:
> >>
> >>f=open("c:/transfer/filename")
> >>for line in f.readlines():
> >> words=line.rstrip().split()
> >>
> >>I get only the last line of the file in words. Could someone explain
> >>why this is happening? There is something about the use of Python here
> >>that I'm not understanding. Thanks for any help.
>
> I suspect you're only *doing* something to the last line.
>
> Consider the following two programs:
>
> f = open("c:/transfer/filename")
> for line in f.readlines():
> words=line.rstrip().split()
> print words
>
> f = open("c:/transfer/filename")
> for line in f.readlines():
> words=line.rstrip().split()
> print words
>
> Both process all the lines in the file but the first only prints out the
> last line while the second prints out all the lines.
>
> Indentation is *critical* in python.
--
"It will be of little avail to the people that the laws are made
by men of their own choice, if the laws be so voluminous that
they cannot be read, or so incoherent that they cannot be
understood; if they be repealed or revised before they are
promulged, or undergo such incessant changes that no man who
knows what the law is to-day can guess what it will be tomorrow.
Law is defined to be a rule of action; but how can that be a
rule, which is little known and less fixed?"
-- James Madison, Federalist #62 (27-Feb-1788)
Rick Pasotto rick@niof.net http://www.niof.net