[Tutor] printing a word in the second line of a text tile

Remco Gerlich scarblac@pino.selwerd.nl
Thu, 18 Oct 2001 12:08:33 +0200

On  0, tonycervone <tonycervone@netzero.net> wrote:
> I want to thank those who replied to my first question, how to print a word in the first line of a text file. How about if I want  to print a word according to any location in the second line of  a text. That is, I want to read the first two lines of a text file, and then print the  first word of the second line of the file. i am using the following:
> inp = open( 'filename')
> line = inp.readlines()
> print = line.split()[position]
> and I get an " attributeError: split" error . thanks again. tony

"print = " isn't allowed, print is a reserver word. You probably wrote it
without the =.

inp.readlines() returns a list of lines. split() is something you can do on
an individual line. So you're almost there.

What you wanted is read a single line, the first one. that's readline, not

inp = open('filename')
line = inp.readline()
print line.split()[position]

If you want to use readlines, it's read

inp = open('filename')
lines = inp.readlines()
print lines[0].split()[position]

But it's a waste to read in all those other lines if you only need the first.

Remco Gerlich