By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,700 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hi,
I having a problem with reading each line from a text file. For example, the
file is a text file named 'test.txt' with the following content :

line 1
line 2
line 3
line 4
line 5

The following script attempts to print out each line :

datafile ="C:\\Classifier\Data\\test.txt"
dataobject = open(datafile,"r")

while dataobject.readline() !="":

line = dataobject.readline()
print line

However, the output from this script is :

line 2

line 4
I'm sure this is a simple problem but I can't figure it after loking up
several reference books and web pages. Any help would be greatly
appreciated.

Regards
Peter

__________________________________________________ _______________
Want to check if your PC is virus-infected? Get a FREE computer virus scan
online from McAfee.
http://clinic.mcafee.com/clinic/ibuy...n.asp?cid=3963
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sun, 19 Oct 2003 20:33:49 -0700, in article
<ma*************************************@python.or g>, 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.
Jul 18 '05 #2

P: n/a
On Sun, 19 Oct 2003 20:33:49 -0700, peter leonard wrote:
while dataobject.readline() !="":
Reads the next line, compares it to the empty string, then throws it
away.
line = dataobject.readline()
Reads the next line and assigns it to the 'line' variable.
print line


Prints out the 'line' variable.

I think you can see where the problem is.

Possibly you want something like this:

while( True ):
line = dataobject.readline()
if( line == "" ):
break
print line

--
\ "Most people don't realize that large pieces of coral, which |
`\ have been painted brown and attached to the skull by common |
_o__) wood screws, can make a child look like a deer." -- Jack Handey |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #3

P: n/a
"peter leonard" <pf*******@hotmail.com> wrote:
datafile ="C:\\Classifier\Data\\test.txt"
dataobject = open(datafile,"r")

while dataobject.readline() !="":

line = dataobject.readline()
print line


The basic problem is that you're calling readline() twice each time
around the loop. Once in the test part of the while (where you test and
then throw away the returned value), and again in the body. Of course
you're only getting every other line! You want to do something like
this:

while 1:
line = dataobject.readline()
if line == "":
break
print line
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.