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. 5 6779
Matthew Wilson wrote: 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.
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
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
At 2003-11-17T15:51:52Z, Matthew Wilson <mw*****@sarcastic-horse.com> writes: 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-----
Matthew Wilson <mw*****@sarcastic-horse.com> wrote in message news:<sl********************@overlook.homelinux.ne t>... 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ken Varn |
last post by:
I want to be able to determine my current line, file, and function in my C#
application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___
macros for getting this, but I cannot find a...
|
by: kangaroo |
last post by:
Hi guys,
when we run a stored proc and it results into an unhandled error, the
error message returned by db2 udb does not contain a line number that
caused an error. This makes it pretty...
|
by: johnivey |
last post by:
I have a large query that I am trying to debug in query analyzer.
However, the errors I get have no line number or reference to where
they are failing. How can I find out what line in the query is...
|
by: Daniel |
last post by:
hi guys
i have never understood this so i am hoping someone can help.
I have had errors in my app before where on closing it down i get a
NullReferenceException error thrown BUT no line number...
|
by: Dan Holmes |
last post by:
Server stack trace:
at IVS.Framework.ControlNumberService.InstallComponent(Identity id,
ControlNumberInfo ctrlNumber) in...
|
by: Jeff Jarrell |
last post by:
I have a block of code that during development is prone to casting errors.
It is mostly a DataReader type thing.
It looks something like this.
_prtPNID = myDLReader.GetString("prtPNID")...
|
by: Lastknight |
last post by:
Hi all
I have googled and get the follwing expression for deleting the particular line of a perl program...
perl -p -i -e"s/.*\n//" filename
I am not able to understand the...
|
by: Horacius ReX |
last post by:
Hi,
I have to search for a string on a big file. Once this string is
found, I would need to get the number of the line in which the string
is located on the file. Do you know how if this is...
|
by: mercuryshipzz |
last post by:
Hi,
My objective is to get the line number of the first occurance of the search pattern.
my test.txt contains:
..... ..................
total rows....
................... ..
|
by: Greg M |
last post by:
I'm not sure how to phrase the question so I'll just show what I'm trying to do.
I have somewhere in a file:
abcdefg1abcdefg
abcdefg abcdefg
abcdefg abcdefg
I used regex to locate where...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |