[Tutor] Blank line added when reading in from file????

alan.gauld@bt.com alan.gauld@bt.com
Wed, 5 Dec 2001 14:39:09 -0000


------_=_NextPart_001_01C17D9A.991EE960
Content-type: text/plain; charset="iso-8859-1"

while p < 4:
   text = in_file.readline()
 
You could just have used:
 
list = in_file.readlines()
 
But list is a bad variable name since its also 
a Python function...it converts things to lists.
 

The problem I have is that the text that is stored in variable 'text' isn't
simply the contents of one line of the file, another blank line is appended
onto the end, which causes problems for the rest of the program, so instead
of this,

I suspect using string.strip() will fix this:
 
lines = in_file.readlines()
for index in range(length(lines)):
   lines[index] = lines[index].strip()
 
BTW The comments elsewhere about HTML refer to the format of your 
email which seems to be in HTML which some folks on the 
tutor list can't read properly (at least their mail client 
can't!).
 
Alan G

------_=_NextPart_001_01C17D9A.991EE960
Content-type: text/html; charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.50.4807.2300" name=GENERATOR></HEAD>
<BODY>
<DIV>while p &lt; 4:<BR>&nbsp;&nbsp; text = in_file.readline()<BR><SPAN 
class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>&nbsp;</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>You could just have used:</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>list = in_file.readlines()</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>But list is a bad variable name since its also </FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>a Python function...it converts things to lists.</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2></FONT></SPAN>&nbsp;</DIV>
<BLOCKQUOTE 
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid">
  <DIV>
  <DIV>The problem I have is that the text that is stored in variable 'text' 
  isn't simply the contents of one line of the file, another blank line is 
  appended onto the end, which causes problems for the rest of the program, so 
  instead of this,</DIV></DIV></BLOCKQUOTE>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>I suspect using string.strip() will fix this:</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>lines = in_file.readlines()</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>for&nbsp;index in range(length(lines)):</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>&nbsp;&nbsp; lines[index] = lines[index].strip()</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>BTW The comments elsewhere about HTML refer to the format of your 
</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>email which seems to be in HTML which some folks on the 
</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>tutor list can't read properly (at least their mail client 
</FONT></SPAN></DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>can't!).</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=610523714-05122001><FONT face="Courier New" color=#0000ff 
size=2>Alan G</FONT></SPAN></DIV></BODY></HTML>

------_=_NextPart_001_01C17D9A.991EE960--