Google Groups Home
Help | Sign in
readlines with line number support?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Nikhil  
View profile
 More options May 14, 2:25 pm
Newsgroups: comp.lang.python
From: Nikhil <mnik...@gmail.com>
Date: Wed, 14 May 2008 23:55:51 +0530
Local: Wed, May 14 2008 2:25 pm
Subject: readlines with line number support?
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul McNett  
View profile
 More options May 14, 2:42 pm
Newsgroups: comp.lang.python
From: Paul McNett <p...@ulmcnett.com>
Date: Wed, 14 May 2008 11:42:27 -0700
Local: Wed, May 14 2008 2:42 pm
Subject: Re: readlines with line number support?

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arnaud Delobelle  
View profile
 More options May 14, 2:45 pm
Newsgroups: comp.lang.python
From: Arnaud Delobelle <arno...@googlemail.com>
Date: Wed, 14 May 2008 19:45:26 +0100
Local: Wed, May 14 2008 2:45 pm
Subject: Re: readlines with line number support?

The standard Python way is using enumerate()

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

--
Arnaud


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nikhil  
View profile
 More options May 14, 2:52 pm
Newsgroups: comp.lang.python
From: Nikhil <mnik...@gmail.com>
Date: Thu, 15 May 2008 00:22:35 +0530
Local: Wed, May 14 2008 2:52 pm
Subject: Re: readlines with line number support?

Oh I did not know enumerate can be used. Thanks Paul and Arnaud.
I will try this.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nikhil  
View profile
 More options May 14, 2:52 pm
Newsgroups: comp.lang.python
From: Nikhil <mnik...@gmail.com>
Date: Thu, 15 May 2008 00:22:53 +0530
Local: Wed, May 14 2008 2:52 pm
Subject: Re: readlines with line number support?

Oh I did not know enumerate can be used. Thanks Paul and Arnaud.
I will try this.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nikhil  
View profile
 More options May 14, 2:54 pm
Newsgroups: comp.lang.python
From: Nikhil <mnik...@gmail.com>
Date: Thu, 15 May 2008 00:24:20 +0530
Local: Wed, May 14 2008 2:54 pm
Subject: Re: readlines with line number support?

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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arnaud Delobelle  
View profile
 More options May 14, 3:20 pm
Newsgroups: comp.lang.python
From: Arnaud Delobelle <arno...@googlemail.com>
Date: Wed, 14 May 2008 20:20:44 +0100
Local: Wed, May 14 2008 3:20 pm
Subject: Re: readlines with line number support?

Nikhil <mnik...@gmail.com> writes:
> 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "enumerate() values starting at 1 vs. 0" by pyt...@bdurham.com
pyt...@bdurham.com  
View profile
 More options May 14, 4:10 pm
Newsgroups: comp.lang.python
From: pyt...@bdurham.com
Date: Wed, 14 May 2008 16:10:24 -0400
Local: Wed, May 14 2008 4:10 pm
Subject: enumerate() values starting at 1 vs. 0
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google