line number var like perl's $.?

Dan Bishop danb_83 at yahoo.com
Mon Nov 17 19:48:24 CET 2003


Matthew Wilson <mwilson at sarcastic-horse.com> wrote in message news:<slrnbrhtdj.gss.mwilson at overlook.homelinux.net>...
> One thing I miss about perl was the builtin $. variable that gets
> increased after each call to perl's file iterator object.  For example:
> 
> while ( my $line = <IN>) {
>     print "$. $line";
> }
...[snip]...
> Is there an equivalent construct in python?  Or are people doing
> something like this:
> 
> linenum = 0
> for line in open('blah.txt'):
>     linenum += 1
>     print linenum, ". ", line
>  
> Better ideas are welcomed.

In Python 2.3, you could use:

for linenum, line in enumerate(file('blah.txt')):
   print linenum, '. ', line




More information about the Python-list mailing list