readlines with line number support?

N

Nikhil

Hi,

I am reading a file with readlines method of the filepointer object
returned by the open function. Along with reading the lines, I also need
to know which line number of the file is read in the loop everytime.
I am sure, the line should have the property/attribute which will say
the line number of the file.

If there is none, do I have to end up using the counter in the loop?

fp = open("file", "r")
lineno = 0
for line in fp.readlines():
print "line number: " + lineno + ": " + line.rstrip()
lineno = lineno + 1
 
P

Paul McNett

Nikhil said:
I am reading a file with readlines method of the filepointer object
returned by the open function. Along with reading the lines, I also need
to know which line number of the file is read in the loop everytime.
I am sure, the line should have the property/attribute which will say
the line number of the file.

If there is none, do I have to end up using the counter in the loop?

fp = open("file", "r")
lineno = 0
for line in fp.readlines():
print "line number: " + lineno + ": " + line.rstrip()
lineno = lineno + 1

Untested:

for lineno, line in enumerate(open("file")):
print "line number: %s : %s" % (idx, line.rstrip())

Note the other stylistic changes, too.


HTH.
Paul
 
A

Arnaud Delobelle

Nikhil said:
Hi,

I am reading a file with readlines method of the filepointer object
returned by the open function. Along with reading the lines, I also
need to know which line number of the file is read in the loop
everytime.
I am sure, the line should have the property/attribute which will say
the line number of the file.

If there is none, do I have to end up using the counter in the loop?

fp = open("file", "r")
lineno = 0
for line in fp.readlines():
print "line number: " + lineno + ": " + line.rstrip()
lineno = lineno + 1

The standard Python way is using enumerate()

for i, line in enumerate(fp):
print "line number: " + lineno + ": " + line.rstrip()
 
N

Nikhil

Arnaud said:
The standard Python way is using enumerate()

for i, line in enumerate(fp):
print "line number: " + lineno + ": " + line.rstrip()
Oh I did not know enumerate can be used. Thanks Paul and Arnaud.
I will try this.
 
N

Nikhil

Arnaud said:
The standard Python way is using enumerate()

for i, line in enumerate(fp):
print "line number: " + lineno + ": " + line.rstrip()
Oh I did not know enumerate can be used. Thanks Paul and Arnaud.
I will try this.
 
N

Nikhil

Arnaud said:
The standard Python way is using enumerate()

for i, line in enumerate(fp):
print "line number: " + lineno + ": " + line.rstrip()

I guess you meant to say :

for lineno, line in enumerate(fp):
print "line number: " + lineno + ": " + line.rstrip()

Thanks.
 
P

python

Arnaud,
I suppose you could redefine enumerate to support an optional argument:

from itertools import izip, count

def enumerate(iterable, start=0):
return izip(count(start), iterable)
[(1, 's'), (2, 'p'), (3, 'a'), (4, 'm')]

Brilliant!!

Thank you,
Malcolm
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top