Re: an enumerate question

2007-03-21 Thread Terry Hancock
[EMAIL PROTECTED] 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 ([EMAIL PROTECTED])
Anansi Spaceworks http://www.AnansiSpaceworks.com

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: an enumerate question

2007-03-20 Thread Paulo da Silva
[EMAIL PROTECTED] 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: an enumerate question

2007-03-19 Thread eight02645999
On Mar 20, 11:00 am, Steven Bethard <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] 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 "", line 2, in 
>  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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: an enumerate question

2007-03-19 Thread Steven Bethard
[EMAIL PROTECTED] 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 "", line 2, in 
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: an enumerate question

2007-03-19 Thread Paul Rubin
[EMAIL PROTECTED] 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: an enumerate question

2007-03-19 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: an enumerate question

2007-03-19 Thread eight02645999
On Mar 20, 9:48 am, Paul Rubin  wrote:
> [EMAIL PROTECTED] 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.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: an enumerate question

2007-03-19 Thread Paul Rubin
[EMAIL PROTECTED] 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?
-- 
http://mail.python.org/mailman/listinfo/python-list


an enumerate question

2007-03-19 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list