[Tutor] printing a word in a line

alan.gauld@bt.com alan.gauld@bt.com
Wed, 17 Oct 2001 11:38:09 +0100


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C156F7.D077AF60
Content-Type: text/plain;
	charset="iso-8859-1"

How do I read a text file and then print the word that is located in a
specific location in the first line of that  text ?  

You probably shouldn't!
If you only want to print that word then just read the first line:
 
inp = popen('filename here')
line = inp.readline()
print line.split()[position]
inp.close()
 
Reading the whole file would be wasteful.
 
Alan G
 

------_=_NextPart_001_01C156F7.D077AF60
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.00.3013.2600" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
  <DIV><FONT face=Arial size=2>How do I read a text file and then print the word 
  that is located in a specific&nbsp;location in the first line&nbsp;of that 
  &nbsp;text ?&nbsp;<SPAN class=910343510-17102001><FONT color=#0000ff 
  face="Courier New">&nbsp;</FONT></SPAN></FONT></DIV></BLOCKQUOTE>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001>You probably shouldn't!</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001>If you only want to print that word then just read the 
first line:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001>inp = popen('filename here')</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001>line = inp.readline()</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001>print line.split()[position]</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001>inp.close()</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001>Reading the whole file would be 
wasteful.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=910343510-17102001>Alan G</SPAN></FONT></DIV>
<DIV 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">&nbsp;</DIV></BODY></HTML>

------_=_NextPart_001_01C156F7.D077AF60--