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

Discussion in 'Python' started by peter leonard, Oct 20, 2003.

  1. 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/campaign.asp?cid=3963
    peter leonard, Oct 20, 2003
    #1
    1. Advertising

  2. peter leonard

    Jules Dubois Guest

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

    On Sun, 19 Oct 2003 20:33:49 -0700, in article
    <>, 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.
    Jules Dubois, Oct 20, 2003
    #2
    1. Advertising

  3. peter leonard

    Ben Finney Guest

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

    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/>
    Ben Finney, Oct 20, 2003
    #3
  4. peter leonard

    Roy Smith Guest

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

    "peter leonard" <> 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
    Roy Smith, Oct 20, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay McGavren
    Replies:
    11
    Views:
    1,113
    Alan Krueger
    Jan 16, 2006
  2. Pettersen, Bjorn S
    Replies:
    2
    Views:
    503
    Christopher Koppler
    Oct 21, 2003
  3. Replies:
    5
    Views:
    997
  4. Marek Stepanek
    Replies:
    12
    Views:
    402
    Peter J. Holzer
    Sep 2, 2006
  5. yelipolok
    Replies:
    4
    Views:
    244
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page