[Tutor] readline() problem
Marilyn Davis
marilyn at deliberate.com
Wed Feb 11 17:33:49 EST 2004
On Wed, 11 Feb 2004, Ron Alvarado 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
The first call to data.readline() will read the first line of the
file, make a string of it, and then rename 'data' to be that
string/line. From then on, all is lost because you have lost your
identifier that points to the file. Next time around the loop, 'data'
is that last line/string and it doesn't know anything about the file
or 'readline'.
The best solution is to rename the file:
file = open('bData.csv', 'r')
num = True
while num != "":
data = file.readline()
print data
However, this will cause an infinite loop because num never becomes
"". So you still have a problem to work out.
Good luck!
Marilyn Davis
>
>
> 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'
> >>>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
More information about the Tutor
mailing list