Looking for a Python Program/Tool That Will Add Line Numbers to atxt File

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

    #16
    Re: Looking for a Python Program/Tool That Will Add Line Numbers to atxt File

    On Feb 15, 8:55 pm, Jeff Schwab <j...@schwabcen ter.comwrote:
    W. Watson wrote:
    See Subject. It's a simple txt file, each line is a Python stmt, but I
    need up to four digits added to each line with a space between the
    number field and the text. Perhaps someone has already done this or
    there's a source on the web for it. I'm not yet into files with Python.
    A sudden need has burst upon me. I'm using Win XP.
    >
    i = 0
    for line in sys.stdin:
    i += 1
    print i, line,
    >
    Or if you want consistent alignment:
    >
    i = 0
    for line in sys.stdin:
    i += 1
    print "%4s" % i, line,
    I like your version best (it's very clean and easy to understand), but
    here's a few more versions...

    from itertools import count

    for i, line in zip(count(1), open('filename. txt')):
    print i, line,

    or with consistent alignment:

    for x in zip(count(1), open('filename. txt')):
    print "%4d %s" % x,

    the latter version gives rise to a one-liner

    open('output.tx t','w').writeli nes('%4d %s' % x for x in
    zip(count(1), open('perms.py' )))

    but as I said, I like the simple for loop the best ;-)

    -- bjorn

    Comment

    • Michael Wronna

      #17
      Re: Looking for a Python Program/Tool That Will Add Line Numbers toa txt File



      On Thu, 14 Feb 2008, W. Watson wrote:
      See Subject. It's a simple txt file, each line is a Python stmt, but I need
      up to four digits added to each line with a space between the number field
      and the text. Perhaps someone has already done this or there's a source on
      the web for it. I'm not yet into files with Python. A sudden need has burst
      upon me. I'm using Win XP.
      --
      Wayne Watson (Nevada City, CA)
      >
      Web Page: <speckledwithSt ars.net>
      >
      Hi Wayne, sorry for that: Change OS, and type cat -n program.py >
      numbered.py
      Just joking, Mike

      Comment

      • William Pursell

        #18
        Re: Looking for a Python Program/Tool That Will Add Line Numbers to atxt File

        On Feb 14, 6:54 am, "W. Watson" <wolf_tra...@in valid.comwrote:
        See Subject. It's a simple txt file, each line is a Python stmt, but I need
        up to four digits added to each line with a space between the number field
        and the text. Perhaps someone has already done this or there's a source on
        the web for it. I'm not yet into files with Python. A sudden need has burst
        upon me. I'm using Win XP.
        Not sure if "Python program/tool" means "a tool or a program
        in Python", but if awk is okay, that's the tool I would use:

        awk '{printf( "%4d %s\n", NR % 10000, $0 )}'

        Comment

        • Reedick, Andrew

          #19
          RE: Looking for a Python Program/Tool That Will Add Line Numbers toatxt File

          -----Original Message-----
          From: python-list-bounces+jr9445= att.com@python. org [mailto:python-
          list-bounces+jr9445= att.com@python. org] On Behalf Of William Pursell
          Sent: Monday, February 18, 2008 8:37 AM
          To: python-list@python.org
          Subject: Re: Looking for a Python Program/Tool That Will Add Line
          Numbers to atxt File

          On Feb 14, 6:54 am, "W. Watson" <wolf_tra...@in valid.comwrote:
          See Subject. It's a simple txt file, each line is a Python stmt, but
          I need
          up to four digits added to each line with a space between the number
          field
          and the text. Perhaps someone has already done this or there's a
          source on
          the web for it. I'm not yet into files with Python. A sudden need
          has
          burst
          upon me. I'm using Win XP.
          Not sure if "Python program/tool" means "a tool or a program
          in Python", but if awk is okay, that's the tool I would use:

          awk '{printf( "%4d %s\n", NR % 10000, $0 )}'

          On a related note, since it's probably easier to install Perl instead of
          awk on a windows box:

          type foo.java | perl -ne "$counter++ ; print sprintf(qq(%4d ), $counter),
          $_;"


          Comment

          Working...