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
--
Thanks,
Nikhil 7 5800
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?
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
Nikhil <mn*****@gmail.comwrites:
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()
--
Arnaud
Arnaud Delobelle wrote:
Nikhil <mn*****@gmail.comwrites:
>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()
Oh I did not know enumerate can be used. Thanks Paul and Arnaud.
I will try this.
Arnaud Delobelle wrote:
Nikhil <mn*****@gmail.comwrites:
>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()
Oh I did not know enumerate can be used. Thanks Paul and Arnaud.
I will try this.
Arnaud Delobelle wrote:
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.
Nikhil <mn*****@gmail.comwrites:
Arnaud Delobelle wrote:
>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()
Yes!
--
Arnaud
Arnaud,
>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:
from itertools import izip, count
def enumerate(iterable, start=0):
return izip(count(start), iterable)
>>list(enumerate('spam', 1))
[(1, 's'), (2, 'p'), (3, 'a'), (4, 'm')]
Brilliant!!
Thank you,
Malcolm This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Leeds, Mark |
last post by:
I did more investigation into my previous problem
and what happens is that my text file has \r for representing
a new line instead
of a \n. is there a way to tell the readlines
function that the...
|
by: Richard |
last post by:
Hi,
Can anyone tell me what the difference is between
for line in file.readlines( ):
and
for line in file:
|
by: Yong Wang |
last post by:
Hi,
I use readlines() to read one data file. Python automatically parses the read contents
into a list of lines. When I used list to print out the 1st line, it is ok. When I use
the list index 2...
|
by: wordsender |
last post by:
Hey guys,
I can't figure this one out, why is this simple script giving me
problems?
logfile=file(r'test.txt','w')
logfile.write('datetime')
test=logfile.readlines()
When I run it I get...
|
by: vch |
last post by:
Does a call to file.readlines() reads all lines at once in the memory?
Are the any reasons, from the performance point of view, to prefer
*while* loop with readline() to *for* loop with readlines()?
|
by: Ross Reyes |
last post by:
HI -
Sorry for maybe a too simple a question but I googled and also checked my
reference O'Reilly Learning Python
book and I did not find a satisfactory answer.
When I use readlines, what...
|
by: wscrsurfdude |
last post by:
Ik have an uml file I want to read with readlines. I have the following
code:
infile = open("out2.txt","r")
for line in infile.readlines():
print line
The print statement just gives the data,...
|
by: tim |
last post by:
I have a module based app that can load many modules at startup. The modules are text based with a '\t' separating the keyword from the definition. The app reads each module to extract the keywords,...
|
by: Wojciech Gryc |
last post by:
Hi,
I'm currently using Python to deal with a fairly large text file (800
MB), which I know has about 85,000 lines of text. I can confirm this
because (1) I built the file myself, and (2)...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |