an enumerate question

Paul Rubin http
Mon Mar 19 22:52:51 EDT 2007


eight02645999 at 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.



More information about the Python-list mailing list