[Tutor] Re: Readlines code.

Cliff Martin camartin@snet.net
Tue Jul 1 21:45:02 2003


--------------030006060102080807010402
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

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.

Cliff Martin

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.

-- "Damn all expurgated books; the dirtiest book of all is the 
expurgated book." -- Walt Whitman Rick Pasotto rick@niof.net 
http://www.niof.net



--------------030006060102080807010402
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
  <title></title>
</head>
<body>
Rick,<br>
<br>
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. &nbsp;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.<br>
<br>
Cliff Martin<br>
<br>
<pre wrap=""><div class="moz-txt-sig">On Mon, Jun 30, 2003 at 09:43:37PM -0400, Cliff Martin wrote:
</div></pre>
<blockquote type="cite">
  <pre wrap=""><span class="moz-txt-citetags">&gt; </span>Hi,
<span class="moz-txt-citetags">&gt; </span>
<span class="moz-txt-citetags">&gt; </span>I'm trying to read in an ASCII file of a fairly large set of data with 
<span class="moz-txt-citetags">&gt; </span>data separated by spaces and each line ending with a linefeed.  If I use 
<span class="moz-txt-citetags">&gt; </span>readlines() I get all the data but each line still has a \n tacked on. 
<span class="moz-txt-citetags">&gt; </span>If I use:
<span class="moz-txt-citetags">&gt; </span>
<span class="moz-txt-citetags">&gt; </span>f=open("c:/transfer/filename")
<span class="moz-txt-citetags">&gt; </span>for line in f.readlines():
<span class="moz-txt-citetags">&gt; </span>       words=line.rstrip().split()
<span class="moz-txt-citetags">&gt; </span>
<span class="moz-txt-citetags">&gt; </span>I get only the last line of the file in words.  Could someone explain 
<span class="moz-txt-citetags">&gt; </span>why this is happening?  There is something about the use of Python here 
<span class="moz-txt-citetags">&gt; </span>that I'm not understanding.  Thanks for any help.
  </pre>
</blockquote>
<pre wrap=""><!---->
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.

<div class="moz-txt-sig">-- 
"Damn all expurgated books; the dirtiest book of all is the expurgated book."
		-- Walt Whitman
    Rick Pasotto    <a class="moz-txt-link-abbreviated"
 href="mailto:rick@niof.net">rick@niof.net</a>    <a
 class="moz-txt-link-freetext" href="http://www.niof.net">http://www.niof.net</a></div></pre>
<br>
</body>
</html>

--------------030006060102080807010402--