Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Beginner question : skips every second line in file when usingreadline()

Reply
Thread Tools

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

 
 
peter leonard
Guest
Posts: n/a
 
      10-20-2003
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


 
Reply With Quote
 
 
 
 
Jules Dubois
Guest
Posts: n/a
 
      10-20-2003
On Sun, 19 Oct 2003 20:33:49 -0700, in article
<(E-Mail Removed)>, 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.
 
Reply With Quote
 
 
 
 
Ben Finney
Guest
Posts: n/a
 
      10-20-2003
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/>
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      10-20-2003
"peter leonard" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strip all but second second line from bottom and then strip that!!!! yelipolok Perl Misc 4 01-27-2010 08:14 AM
Regular expression that skips single line comments? martinjamesevans@gmail.com Python 5 01-20-2009 07:51 AM
Beginner: read $array with line breaks line by line Marek Stepanek Perl Misc 12 09-02-2006 10:27 AM
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
RE: Beginner question : skips every second line in file when usingreadline() Pettersen, Bjorn S Python 2 10-21-2003 01:34 AM



Advertisments