[Tutor] printing a word in the second line of a text tile
alan.gauld@bt.com
alan.gauld@bt.com
Fri, 19 Oct 2001 12:49:42 +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_01C15894.2404C120
Content-Type: text/plain;
charset="iso-8859-1"
inp = open( 'filename')
line = inp.readlines()
print = line.split()[position]
and I get an " attributeError: split" error . thanks again. tony
Because you are now reading lines, plural, so
the variable line is actually a list. But split
is a method of string objects.
So you need to extract the string from the list
either by iterating over the list:
lines = inp.readlines()
for line in lines:
# do something here
Or by indexing for a spercific position:
print line[linenumber].split()[position]
HTH,
Alan G.
------_=_NextPart_001_01C15894.2404C120
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 dir=ltr
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
<DIV><FONT face=Arial size=2>inp = open( 'filename')</FONT></DIV>
<DIV><FONT face=Arial size=2>line = inp.readlines()</FONT></DIV>
<DIV><FONT face=Arial size=2>print = line.split()[position]</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial>and I get an " attributeError: split" error
. thanks again. tony<FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001> </SPAN></FONT></FONT></FONT></DIV></BLOCKQUOTE>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>Because you are now reading lines, plural, so
</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>the variable line is actually a list. But split
</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>is a method of string
objects. </SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001></SPAN></FONT></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>So you need to extract the string from the
list</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>either by iterating over the
list:</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001></SPAN></FONT></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>lines =
inp.readlines()</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>for line in lines:
</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001> # do something
here</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001></SPAN></FONT></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>Or by indexing for a spercific
position:</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001></SPAN></FONT></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>print
line[linenumber].split()[position]</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001></SPAN></FONT></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>HTH,</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001></SPAN></FONT></FONT></FONT> </DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff face="Courier New"><SPAN
class=410464611-19102001>Alan G.</SPAN></FONT></FONT></FONT></DIV>
<BLOCKQUOTE dir=ltr
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
<BLOCKQUOTE dir=ltr
style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
<BLOCKQUOTE dir=ltr
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
<BLOCKQUOTE dir=ltr
style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
<DIV
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px"> </DIV></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BODY></HTML>
------_=_NextPart_001_01C15894.2404C120--