an enumerate question

E

eight02645999

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
 
P

Paul Rubin

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?
 
E

eight02645999

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.
 
S

skip

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
 
P

Paul Rubin

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.
 
S

Steven Bethard

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::
... 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::
... 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
 
E

eight02645999

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

... 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::

... 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.
 
P

Paulo da Silva

(e-mail address removed) 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
 
T

Terry Hancock

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top