line number var like perl's $.?

M

Matthew Wilson

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";
}

or, more perlish:

while (<IN>) {
print "$. $_";
}

Tracking line numbers is such a common thing to do when parsing files
that it makes sense for there to be a builtin for it.

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

anton muhin

Matthew said:
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";
}

or, more perlish:

while (<IN>) {
print "$. $_";
}

Tracking line numbers is such a common thing to do when parsing files
that it makes sense for there to be a builtin for it.

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.

If you upgrade to 2.3, you can use enumerate built-in:

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

should work (I didn't test it however)

Otherwise you can create enumerate yourself:

from __future__ import generators

def enumerate(it):
n = 0
for e in it:
yield n, e
n += 1

Or to use xrange trick:

import sys
for no, line in zip(xrange(0, sys.maxint), file('blah.txt')):
print no, line

(not tested either).

regards,
anton.
 
A

Alex Martelli

Matthew Wilson wrote:
...
while (<IN>) {
print "$. $_";
}

Tracking line numbers is such a common thing to do when parsing files
that it makes sense for there to be a builtin for it.

Python disagrees with you, and prefers to keep things in modules, in
most cases, rather than shoveling them wholesale into the builtins.

The module that's roughly equivalent to perl's "while(<something>)" is
named fileinput. The task you sketch above, in Python, would normally
be coded more or less as follows:


import fileinput

for line in fileinput.input("readme.html"):
print fileinput.lineno(), line,


There are several other possible approaches, but I find that fileinput
generally affords the smoothest translation of perl input idioms.


Alex
 
K

Kirk Strauser

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2003-11-17T15:51:52Z said:
Tracking line numbers is such a common thing to do when parsing files that
it makes sense for there to be a builtin for it.

OTOH, I don't know that I've ever needed to track line numbers, other than
to do something like 'print "Lines:", linenum' after reading a bunch of
stuff from stdin.

I only mention this to illustrate that what's common to you is not
necessarily common to others. Your essential feature is another's cruft.
- --
Kirk Strauser
The Strauser Group
Open. Solutions. Simple.
http://www.strausergroup.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/uQqh5sRg+Y0CpvERAm6XAJwOsjML/se7yf6PGeNmHEqc8PvnmACcCllx
rkneNtVmVNKNiW3QPQ0i21Y=
=oaZV
-----END PGP SIGNATURE-----
 
D

Dan Bishop

Matthew Wilson said:
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
 
M

Miki Tebeka

Hello Matthew,
linenum = 0
for line in open('blah.txt'):
linenum += 1
print linenum, ". ", line
In 2.3 you can use "enumerate"
for lnum, line in enumerate(open("blah.txt")):
print lnum, ".", line

HTH.
Miki
 

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
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top