an enumerate question

Discussion in 'Python' started by eight02645999@yahoo.com, Mar 20, 2007.

  1. Guest

    hi
    say i want to enumerate lines of a file
    eg
    for n,l in enumerate(open("file")):
    # print next line ie

    is there a way to print out the next line from current line using the
    above?.
    Or do i have to do a readlines() first to get it into a list eg
    d = open("file").readlines()
    for n, l in enumerate(d):
    print d[n+1]

    thanks
     
    , Mar 20, 2007
    #1
    1. Advertising

  2. Paul Rubin Guest

    writes:
    > hi
    > say i want to enumerate lines of a file
    > eg
    > for n,l in enumerate(open("file")):
    > # print next line ie
    >
    > is there a way to print out the next line from current line using the
    > above?.


    I don't understand what you're trying to do. You mean you're
    trying to print all lines except the first one?
     
    Paul Rubin, Mar 20, 2007
    #2
    1. Advertising

  3. Guest

    On Mar 20, 9:48 am, Paul Rubin <http://> wrote:
    > writes:
    > > hi
    > > say i want to enumerate lines of a file
    > > eg
    > > for n,l in enumerate(open("file")):
    > > # print next line ie

    >
    > > is there a way to print out the next line from current line using the
    > > above?.

    >
    > I don't understand what you're trying to do. You mean you're
    > trying to print all lines except the first one?


    thanks for replying. sorry i make clear again.
    say
    for n,l in enumerate(open("file")):
    print n,l # this prints current line
    print next line in this current iteration of the loop.
    hope you can understand now.
    thanks.
     
    , Mar 20, 2007
    #3
  4. Guest

    eight> thanks for replying. sorry i make clear again.
    eight> say
    eight> for n,l in enumerate(open("file")):
    eight> print n,l # this prints current line
    eight> print next line in this current iteration of the loop.

    Off the top of my head, I'd try something like:

    class TwoLiner:
    def __init__(self, f):
    self.f = f

    def __iter__(self):
    return self

    def next(self):
    line1 = self.f.next()
    line2 = self.f.next()
    return (line1, line2)

    for n, (line1, line2) in enumerate(TwoLiner(open("file")):
    print n, line1
    print line2

    Note that this has and end-of-file problem. When your file contains an odd
    number of lines, at the end of the file TwoLiner.next will raise
    StopIteration during the second self.f.next() call and you'll lose the last
    line of the file. You can work around it with something like this:

    def next(self):
    line1 = self.f.next()
    try:
    line2 = self.f.next()
    except StopIteration:
    line2 = None
    return (line1, line2)

    then when using it you have to test line2 for None:

    for n, (line1, line2) in enumerate(TwoLiner(open("file")):
    print n, line1
    if line2 is not None:
    print line2

    Skip
     
    , Mar 20, 2007
    #4
  5. Paul Rubin Guest

    writes:
    > for n,l in enumerate(open("file")):
    > print n,l # this prints current line
    > print next line in this current iteration of the loop.
    > hope you can understand now.


    I see. It just seemed a little weird. If the file contains
    first line
    second line
    third line

    You want the output:

    1 first line
    second line
    2 second line
    third line
    3 third line

    Is that right?

    Anyway all the ways I can think of to do it are at least somewhat
    messy. Skip suggested one. It's easier if you're sure the file
    is small enough to fit all its lines completely into memory.
     
    Paul Rubin, Mar 20, 2007
    #5
  6. wrote:
    > for n,l in enumerate(open("file")):
    > print n,l # this prints current line
    > print next line in this current iteration of the loop.


    Depends what you want to happen when you request "next". If you want to
    renumber the lines, you can call .next() on the iterator::

    >>> open('temp.txt', 'w').write('1\n2\n3\n4\n5\n6\n7\n')
    >>> lines_iter = open('temp.txt')
    >>> for i, line in enumerate(lines_iter):

    ... print 'LINE %i, %r %r' % (i, line, lines_iter.next())
    ...
    LINE 0, '1\n' '2\n'
    LINE 1, '3\n' '4\n'
    LINE 2, '5\n' '6\n'
    Traceback (most recent call last):
    File "<interactive input>", line 2, in <module>
    StopIteration

    If you want to peek ahead without removing the line from the iterator,
    check out this recipe::

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373

    Which allows code like::

    >>> lines_iter = peekable(open('temp.txt'))
    >>> for i, line in enumerate(lines_iter):

    ... print 'LINE %i, %r %r' % (i, line, lines_iter.peek())
    ...
    LINE 0, '1\n' '2\n'
    LINE 1, '2\n' '3\n'
    LINE 2, '3\n' '4\n'
    LINE 3, '4\n' '5\n'
    LINE 4, '5\n' '6\n'
    LINE 5, '6\n' '7\n'
    Traceback (most recent call last):
    ...
    StopIteration

    (Note that the recipe doesn't try to catch the StopIteration, but if you
    want that suppressed, it should be a pretty simple change.)

    STeVe
     
    Steven Bethard, Mar 20, 2007
    #6
  7. Guest

    On Mar 20, 11:00 am, Steven Bethard <> wrote:
    > wrote:
    > > for n,l in enumerate(open("file")):
    > > print n,l # this prints current line
    > > print next line in this current iteration of the loop.

    >
    > Depends what you want to happen when you request "next". If you want to
    > renumber the lines, you can call .next() on the iterator::
    >
    > >>> open('temp.txt', 'w').write('1\n2\n3\n4\n5\n6\n7\n')
    > >>> lines_iter = open('temp.txt')
    > >>> for i, line in enumerate(lines_iter):

    > ... print 'LINE %i, %r %r' % (i, line, lines_iter.next())
    > ...
    > LINE 0, '1\n' '2\n'
    > LINE 1, '3\n' '4\n'
    > LINE 2, '5\n' '6\n'
    > Traceback (most recent call last):
    > File "<interactive input>", line 2, in <module>
    > StopIteration
    >
    > If you want to peek ahead without removing the line from the iterator,
    > check out this recipe::
    >
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373
    >
    > Which allows code like::
    >
    > >>> lines_iter = peekable(open('temp.txt'))
    > >>> for i, line in enumerate(lines_iter):

    > ... print 'LINE %i, %r %r' % (i, line, lines_iter.peek())
    > ...
    > LINE 0, '1\n' '2\n'
    > LINE 1, '2\n' '3\n'
    > LINE 2, '3\n' '4\n'
    > LINE 3, '4\n' '5\n'
    > LINE 4, '5\n' '6\n'
    > LINE 5, '6\n' '7\n'
    > Traceback (most recent call last):
    > ...
    > StopIteration
    >
    > (Note that the recipe doesn't try to catch the StopIteration, but if you
    > want that suppressed, it should be a pretty simple change.)
    >
    > STeVe


    thanks, lines_iter.next() is what i need at the moment.
     
    , Mar 20, 2007
    #7
  8. escreveu:
    > hi
    > say i want to enumerate lines of a file
    > eg
    > for n,l in enumerate(open("file")):
    > # print next line ie
    >
    > is there a way to print out the next line from current line using the
    > above?.
    > Or do i have to do a readlines() first to get it into a list eg
    > d = open("file").readlines()
    > for n, l in enumerate(d):
    > print d[n+1]
    >
    > thanks
    >

    for n,l in enumerate(file("file")):
    print n,l[:-1] # the :-1 is to drop the \n - print n,l, also works (end
    with ',').
    HTH
    Paulo
     
    Paulo da Silva, Mar 20, 2007
    #8
  9. wrote:
    > say i want to enumerate lines of a file
    > eg
    > for n,l in enumerate(open("file")):
    > # print next line ie


    I think you'd find it much easier to move your frame of reference one
    line forward and think in terms of remembering the previous line, e.g.:

    for n,curr in enumerate(open("file")):
    if n>1:
    print n,curr
    print m,prev
    m,prev = n,curr

    Of course, if the file isn't so big, then you could use readlines as you
    mention.

    Cheers,
    Terry

    --
    Terry Hancock ()
    Anansi Spaceworks http://www.AnansiSpaceworks.com
     
    Terry Hancock, Mar 21, 2007
    #9
    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. poi

    Enumerate Roles?

    poi, Nov 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    520
    Oliver
    Nov 15, 2003
  2. shiv
    Replies:
    3
    Views:
    6,816
    S. Justin Gengo
    Dec 3, 2003
  3. localhost

    Enumerate Control Attributes?

    localhost, Dec 16, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    630
    localhost
    Dec 22, 2003
  4. Pekka Niiranen
    Replies:
    5
    Views:
    360
    Pekka Niiranen
    Oct 31, 2004
  5. Gregory Petrosyan

    enumerate() question

    Gregory Petrosyan, May 22, 2006, in forum: Python
    Replies:
    6
    Views:
    297
    Robert Kern
    May 22, 2006
Loading...

Share This Page