Beginner question : skips every second line in file when using readline()

Jules Dubois bogus at invalid.tld
Mon Oct 20 00:55:30 EDT 2003


On Sun, 19 Oct 2003 20:33:49 -0700, in article
<mailman.226.1066620834.2192.python-list at python.org>, peter leonard wrote:

> while dataobject.readline() !="":
> 
>         line = dataobject.readline()
>         print line
> 
> However, the output from this script is :
> 
> line 2
> 
> line 4

You're reading a line in the "while:" statement by calling readline(), but
the line being read isn't used.  Then, you call readline() again in the
body of the loop.  That's the only input you're going to see in your
output, the even-numbered lines.

Do something like this instead:

    for line in dataobject.xreadlines():
        print line

I'm no Python expert, so there may be a better way.  The code above works
for me.




More information about the Python-list mailing list