By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,995 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,995 IT Pros & Developers. It's quick & easy.

an enumerate question

P: n/a
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 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ei***********@yahoo.com 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?
Mar 20 '07 #2

P: n/a
On Mar 20, 9:48 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
eight02645...@yahoo.com 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 '07 #3

P: n/a

eightthanks for replying. sorry i make clear again.
eightsay
eightfor 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 '07 #4

P: n/a
ei***********@yahoo.com 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.
Mar 20 '07 #5

P: n/a
ei***********@yahoo.com 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/Coo.../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
Mar 20 '07 #6

P: n/a
On Mar 20, 11:00 am, Steven Bethard <steven.beth...@gmail.comwrote:
eight02645...@yahoo.com 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/Coo.../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 '07 #7

P: n/a
ei***********@yahoo.com 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

Mar 20 '07 #8

P: n/a
ei***********@yahoo.com 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 (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Mar 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.