Edit text file to add row numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Boeileh
    New Member
    • Oct 2008
    • 4

    Edit text file to add row numbers

    Hi guys!
    I just arrived here on this forum, and I hope you people can help me a bit with Python.

    What I want is to edit a few large text files, by adding in front of each row the concerning rownumber. What I find out myself (not much..) is:

    infile = open('infile.tx t')
    outfile = open('outfile.t xt','w')
    for i in range(len(infil e)):
    infile[i] = str(i) + ' ' + cb[i]
    infile.close()
    outfile.close()

    Am I close :) ? Thanks for helping!
  • Boeileh
    New Member
    • Oct 2008
    • 4

    #2
    I now have this, but it is still not working..

    file = open("infile.tx t")
    intext = file.read()
    file.close()

    for i in range(len(intex t)):
    outtext[i] = str(i) + ' ' + intext[i]

    file = open("outfile.t xt","w")
    file.write(outt ext)
    file.close()

    Comment

    • freddukes
      New Member
      • Sep 2008
      • 18

      #3
      Hmmm... look into <open file>.readlines () and similarly <open file>.writeline s()

      -freddukes

      Comment

      • Boeileh
        New Member
        • Oct 2008
        • 4

        #4
        Originally posted by freddukes
        Hmmm... look into <open file>.readlines () and similarly <open file>.writeline s()

        -freddukes
        Their is still something going wrong. This is the code I have now:
        Code:
        infile=open('infile.txt', 'r')
        lines=infile.readlines()
        infile.close()
        inlist=list(lines)
        outtext = {}
        for i in range(len(inlist)):
            outtext[i] = str(i) + ' ' + inlist[i]
        outfile = open("outfile.txt","w")
        outfile.writelines(str(outtext))
        outfile.close()

        If this is the input:
        a
        b
        c

        I want to have this:
        0 a
        1 b
        2 c

        What I receive with the code above is:
        {0: '0 a\n', 1: '1 b\n', 2: '2 c\n'}

        Who can give me the sollution? Thanks!

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Given f, an open file object or list of lines from a file returned by file method readlines(), this one-liner will create a list of lines suitable for output to a file.
          Code:
          output = ['%d %s' % (i, line) for i, line in enumerate(f)]
          Hint: Use the string method join() to write the modified data to disk.

          Comment

          • Boeileh
            New Member
            • Oct 2008
            • 4

            #6
            It works! Thanks everybody!

            This is the final code:
            Code:
            infile=open('2_2000.txt', 'r')
            lines=infile.readlines()
            infile.close()
            outtext = ['%d %s' % (i, line) for i, line in enumerate(lines)]
            outfile = open("3_2000.txt","w")
            outfile.writelines(str("".join(outtext)))
            outfile.close()

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by Boeileh
              It works! Thanks everybody!

              This is the final code:
              Code:
              infile=open('2_2000.txt', 'r')
              lines=infile.readlines()
              infile.close()
              outtext = ['%d %s' % (i, line) for i, line in enumerate(lines)]
              outfile = open("3_2000.txt","w")
              outfile.writelines(str("".join(outtext)))
              outfile.close()
              You can eliminate str() in the next to last line. Since join() returns one string, you can use f.write() or eliminate join().[code=Python]outfile.write(" ".join(outtext) )[/code]OR[code=Python]outfile.writeli nes(outtext)[/code]

              Comment

              Working...