Reading text files where last line has no EOL

Discussion in 'Python' started by BlueBird, Sep 17, 2007.

  1. BlueBird

    BlueBird Guest

    I tried and failed to read text files where the last line does not
    contain proper EOL. For my tests, I use a file that I create with the
    equivalent of :

    open('toto', 'w').write( '1234\n4567\n89AB' )


    My reading code looks like this :

    l = f.readline()
    while len(l):
    self.appendLine( l )
    l = f.readline()

    The last line is not returned (89AB) is never returned.

    I tried with "for l in f" with similar results.

    I read the doc :

    In order to make a for loop the most efficient way of looping over the
    lines of a file (a very common operation), the next() method uses a
    hidden read-ahead buffer. As a consequence of using a read-ahead
    buffer, combining next() with other file methods (like readline())
    does not work right. However, using seek() to reposition the file to
    an absolute position will flush the read-ahead buffer. New in version
    2.3.

    I've tried to do a f.seek( f.tell() ) but that did not help.

    So how am I supposed to fetch that last line ?
    BlueBird, Sep 17, 2007
    #1
    1. Advertising

  2. BlueBird

    Steve Holden Guest

    BlueBird wrote:
    > I tried and failed to read text files where the last line does not
    > contain proper EOL. For my tests, I use a file that I create with the
    > equivalent of :
    >
    > open('toto', 'w').write( '1234\n4567\n89AB' )
    >
    >
    > My reading code looks like this :
    >
    > l = f.readline()
    > while len(l):
    > self.appendLine( l )
    > l = f.readline()
    >
    > The last line is not returned (89AB) is never returned.
    >
    > I tried with "for l in f" with similar results.
    >
    > I read the doc :
    >
    > In order to make a for loop the most efficient way of looping over the
    > lines of a file (a very common operation), the next() method uses a
    > hidden read-ahead buffer. As a consequence of using a read-ahead
    > buffer, combining next() with other file methods (like readline())
    > does not work right. However, using seek() to reposition the file to
    > an absolute position will flush the read-ahead buffer. New in version
    > 2.3.
    >
    > I've tried to do a f.seek( f.tell() ) but that did not help.
    >
    > So how am I supposed to fetch that last line ?
    >

    What version of Python are you using, and on what platform? WJFFM on
    2.5.1/Cygwin:

    >>> open('toto', 'w').write( '1234\n4567\n89AB' )
    >>> for l in open('toto'):

    .... print l
    ....
    1234

    4567

    89AB
    >>>


    You will observe that the last line is presented, but correctly does not
    include a trailing line feed.

    regards
    Steve

    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden

    Sorry, the dog ate my .sigline
    Steve Holden, Sep 17, 2007
    #2
    1. Advertising

  3. BlueBird

    Steve Holden Guest

    BlueBird wrote:
    > I tried and failed to read text files where the last line does not
    > contain proper EOL. For my tests, I use a file that I create with the
    > equivalent of :
    >
    > open('toto', 'w').write( '1234\n4567\n89AB' )
    >
    >
    > My reading code looks like this :
    >
    > l = f.readline()
    > while len(l):
    > self.appendLine( l )
    > l = f.readline()
    >
    > The last line is not returned (89AB) is never returned.
    >
    > I tried with "for l in f" with similar results.
    >
    > I read the doc :
    >
    > In order to make a for loop the most efficient way of looping over the
    > lines of a file (a very common operation), the next() method uses a
    > hidden read-ahead buffer. As a consequence of using a read-ahead
    > buffer, combining next() with other file methods (like readline())
    > does not work right. However, using seek() to reposition the file to
    > an absolute position will flush the read-ahead buffer. New in version
    > 2.3.
    >
    > I've tried to do a f.seek( f.tell() ) but that did not help.
    >
    > So how am I supposed to fetch that last line ?
    >

    What version of Python are you using, and on what platform? WJFFM on
    2.5.1/Cygwin:

    >>> open('toto', 'w').write( '1234\n4567\n89AB' )
    >>> for l in open('toto'):

    .... print l
    ....
    1234

    4567

    89AB
    >>>


    You will observe that the last line is presented, but correctly does not
    include a trailing line feed.

    regards
    Steve

    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden

    Sorry, the dog ate my .sigline
    Steve Holden, Sep 17, 2007
    #3
  4. BlueBird

    BlueBird Guest

    On 17 sep, 13:24, Steve Holden <> wrote:
    > BlueBird wrote:
    > > I tried and failed to read text files where the last line does not
    > > contain proper EOL. For my tests, I use a file that I create with the
    > > equivalent of :

    >
    > > open('toto', 'w').write( '1234\n4567\n89AB' )

    >
    > > My reading code looks like this :

    >
    > > l = f.readline()
    > > while len(l):
    > > self.appendLine( l )
    > > l = f.readline()

    >
    > > The last line is not returned (89AB) is never returned.

    >
    > > I tried with "for l in f" with similar results.

    >
    > > I read the doc :

    >
    > > In order to make a for loop the most efficient way of looping over the
    > > lines of a file (a very common operation), the next() method uses a
    > > hidden read-ahead buffer. As a consequence of using a read-ahead
    > > buffer, combining next() with other file methods (like readline())
    > > does not work right. However, using seek() to reposition the file to
    > > an absolute position will flush the read-ahead buffer. New in version
    > > 2.3.

    >
    > > I've tried to do a f.seek( f.tell() ) but that did not help.

    >
    > > So how am I supposed to fetch that last line ?

    >
    > What version of Python are you using, and on what platform? WJFFM on
    > 2.5.1/Cygwin:
    >
    > >>> open('toto', 'w').write( '1234\n4567\n89AB' )
    > >>> for l in open('toto'):

    > ... print l
    > ...
    > 1234
    >
    > 4567
    >
    > 89AB
    > >>>

    >
    > You will observe that the last line is presented, but correctly does not
    > include a trailing line feed.
    >


    Oooooooops. It was a stupid bug in my script: textLine[:-
    int(textLine[-1]=='\n')] is not what I want (the real code was not a
    one-liner)! The documentation made me think that something wrong was
    going on with python but I should have known better.

    Thanks.
    BlueBird, Sep 17, 2007
    #4
  5. En Mon, 17 Sep 2007 09:02:38 -0300, BlueBird <>
    escribi�:

    > Oooooooops. It was a stupid bug in my script: textLine[:-
    > int(textLine[-1]=='\n')] is not what I want (the real code was not a
    > one-liner)! The documentation made me think that something wrong was
    > going on with python but I should have known better.


    If that expression is supposed to remove the trailing newline, I suggest
    using
    textLine = textLine.rstrip('\n')

    --
    Gabriel Genellina
    Gabriel Genellina, Sep 17, 2007
    #5
    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. Nick Wain
    Replies:
    1
    Views:
    336
    Diez B. Roggisch
    Jan 18, 2006
  2. xyz
    Replies:
    3
    Views:
    609
  3. Robin Wenger
    Replies:
    191
    Views:
    3,196
  4. ZZZupan
    Replies:
    3
    Views:
    194
    ZZZupan
    Sep 22, 2004
  5. Une Bévue

    merging files with CR as EOL ?

    Une Bévue, Oct 18, 2006, in forum: Ruby
    Replies:
    3
    Views:
    81
    Une Bévue
    Oct 18, 2006
Loading...

Share This Page