[Tutor] readline() problem

Karl Pflästerer sigurd at 12move.de
Wed Feb 11 17:37:59 EST 2004

On 11 Feb 2004, Ron Alvarado <- rha207 at worldnet.att.net wrote:

> Here's what I'm getting when I try readline(). What an I doing wrong?

>>>> data = open('bData.csv', 'r')
>>>> num = True
>>>> while num != "":
>  data = data.readline()
>  print data

> Part number Description Item Cost 1104 1105 1118

> Traceback (most recent call last):
>   File "<pyshell#7>", line 2, in -toplevel-
>     data = data.readline()
> AttributeError: 'str' object has no attribute 'readline'

Several things here are wrong.
(a) You have an endless loop (or do you set num somewhere in your code)
(b) First data is file object; then you assign to data (the same name!)
    the value of date.readline() which is a string.  data gets printed
    and the second calls causes this error (since data is now a string).
(c) Nowadays there is a better idiom in Python to iterate over the lines
    of a file: for var in file: ....

So your code could be written as:

data = file('bData.csv')

for line in data:
    print line

Please do *not* send copies of replies to me.
I read the list

More information about the Tutor mailing list