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
Nikhil wrote: > 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?
> 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?
>> 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?
>> 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?
>> Is there any way to have enumerate() start at 1 vs. 0?
>> The problem with starting at 0 is that many things in the real world >> begin at 1 - like line numbers or labels in a list. > I suppose you could redefine enumerate to support an optional argument: