readlines with line number support?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nikhil

    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
  • Paul McNett

    #2
    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

    Comment

    • Arnaud Delobelle

      #3
      Re: readlines with line number support?

      Nikhil <mnikhil@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

      Comment

      • Nikhil

        #4
        Re: readlines with line number support?

        Arnaud Delobelle wrote:
        Nikhil <mnikhil@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.

        Comment

        • Nikhil

          #5
          Re: readlines with line number support?

          Arnaud Delobelle wrote:
          Nikhil <mnikhil@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.

          Comment

          • Nikhil

            #6
            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.

            Comment

            • Arnaud Delobelle

              #7
              Re: readlines with line number support?

              Nikhil <mnikhil@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

              Comment

              • python@bdurham.com

                #8
                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(itera ble, start=0):
                return izip(count(star t), iterable)
                >
                >>list(enumerat e('spam', 1))
                [(1, 's'), (2, 'p'), (3, 'a'), (4, 'm')]
                Brilliant!!

                Thank you,
                Malcolm

                Comment

                Working...