edit a text file with Python.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ironmonkey69
    New Member
    • Jul 2007
    • 43

    edit a text file with Python.

    I'm learning Python and I need some help. I have a text file with 12 columns filled with numbers. I need to write a script that would be able to select a column and zero out all the values in that column. I don't need to worry about grabbing the file or saving it because I'm useing an extrnal program to do all of that. I just need help with that part that zero's otu the specified column.
  • ironmonkey69
    New Member
    • Jul 2007
    • 43

    #2
    also that text file has a some text before the columns in separate line.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by ironmonkey69
      I'm learning Python and I need some help. I have a text file with 12 columns filled with numbers. I need to write a script that would be able to select a column and zero out all the values in that column. I don't need to worry about grabbing the file or saving it because I'm useing an extrnal program to do all of that. I just need help with that part that zero's otu the specified column.
      What does your external program return to you? A list, dictionary, set, string? If it is a dictionary:[code=Python]>>> dd = {'line1':[1,1,1,1], 'line2':[2,2,2,2], 'line3':[3,3,3,3]}
      >>> columnzero = 1
      >>> for key in dd:
      ... dd[key][columnzero] = 0
      ...
      >>> dd
      {'line3': [3, 0, 3, 3], 'line2': [2, 0, 2, 2], 'line1': [1, 0, 1, 1]}
      >>> [/code]

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Originally posted by ironmonkey69
        I'm learning Python and I need some help. I have a text file with 12 columns filled with numbers. I need to write a script that would be able to select a column and zero out all the values in that column. I don't need to worry about grabbing the file or saving it because I'm useing an extrnal program to do all of that. I just need help with that part that zero's otu the specified column.
        why don't you show a sample input file, a sample output file that you want?

        Comment

        • ironmonkey69
          New Member
          • Jul 2007
          • 43

          #5
          How can you tell if it is a list, dictionary, set, string?

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by ironmonkey69
            How can you tell if it is a list, dictionary, set, string?
            When the object is printed to the screen or your IDE interactive window, you should be able to tell. We need more info.

            Comment

            • ironmonkey69
              New Member
              • Jul 2007
              • 43

              #7
              The actual text file is really long (a total of 255 rows), but here is what the start of the input file looks like:

              #Number of Bits
              12
              #Data
              0 0 0 0 0 0 0 0 0 0 0 0
              12 5 3 4 6 4 5 4 7 5 5 10
              24 9 7 7 13 7 9 9 14 10 10 20

              These are the first three rows,

              Here is the desired output(also first three rows:

              #Number of Bits
              12
              #Data
              0 0 0 0 0 0 0 0 0 0 0 0
              12 0 3 4 6 4 5 4 7 5 5 10
              24 0 7 7 13 7 9 9 14 10 10 20

              in this I wanted the second column to be zero'd out

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Originally posted by ironmonkey69
                The actual text file is really long (a total of 255 rows), but here is what the start of the input file looks like:

                #Number of Bits
                12
                #Data
                0 0 0 0 0 0 0 0 0 0 0 0
                12 5 3 4 6 4 5 4 7 5 5 10
                24 9 7 7 13 7 9 9 14 10 10 20

                These are the first three rows,

                Here is the desired output(also first three rows:

                #Number of Bits
                12
                #Data
                0 0 0 0 0 0 0 0 0 0 0 0
                12 0 3 4 6 4 5 4 7 5 5 10
                24 0 7 7 13 7 9 9 14 10 10 20

                in this I wanted the second column to be zero'd out
                The following code will read the file, zero the second list elements, and write the date out to another file:[code=Python]
                def nthzero(dataLis t, nth, n):
                '''
                Replace the nth element of each list in the data list with 'n'
                '''
                for item in dataList:
                item[nth] = n
                return dataList

                fn = 'infile.txt'
                f = open(fn)

                s = f.next()
                prefix = s
                while s.strip() != '#Data':
                s = f.next()
                prefix += s

                lineList = [line.strip().sp lit() for line in f]

                f.close()
                elem = 1
                repl = '0'
                lineList = nthzero(lineLis t, elem, repl)

                fn1 = 'outfile.txt'
                f = open(fn1, 'w')
                outList = []
                for line in lineList:
                outList.append( ' '.join(line))

                f.write('%s%s' % (prefix, '\n'.join(outLi st)))
                f.close()[/code]

                Comment

                • ironmonkey69
                  New Member
                  • Jul 2007
                  • 43

                  #9
                  how can I get this code to zero out more than one column?

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Originally posted by ironmonkey69
                    how can I get this code to zero out more than one column?
                    [code=Python]
                    def nthzero(dataLis t, nthList, n):
                    '''
                    Replace the nth element of each list in dataList with 'n'
                    '''
                    for nth in nthList:
                    for item in dataList:
                    try:
                    item[nth] = n
                    print dataList
                    except IndexError, n:
                    print n
                    return dataList

                    fn = 'indata.txt'
                    f = open(fn)

                    s = f.next()
                    prefix = s
                    while s.strip() != '#Data':
                    s = f.next()
                    prefix += s

                    lineList = [line.strip().sp lit() for line in f]

                    f.close()
                    elem = [1,3,5,8,12]
                    repl = '0'
                    lineList = nthzero(lineLis t, elem, repl)
                    print lineList
                    fn1 = 'outdata.txt'
                    f = open(fn1, 'w')
                    outList = []
                    for line in lineList:
                    outList.append( ' '.join(line))

                    f.write('%s%s' % (prefix, '\n'.join(outLi st)))
                    f.close()[/code]

                    Comment

                    • ghostdog74
                      Recognized Expert Contributor
                      • Apr 2006
                      • 511

                      #11
                      Originally posted by ironmonkey69
                      The actual text file is really long (a total of 255 rows), but here is what the start of the input file looks like:

                      #Number of Bits
                      12
                      #Data
                      0 0 0 0 0 0 0 0 0 0 0 0
                      12 5 3 4 6 4 5 4 7 5 5 10
                      24 9 7 7 13 7 9 9 14 10 10 20

                      These are the first three rows,

                      Here is the desired output(also first three rows:

                      #Number of Bits
                      12
                      #Data
                      0 0 0 0 0 0 0 0 0 0 0 0
                      12 0 3 4 6 4 5 4 7 5 5 10
                      24 0 7 7 13 7 9 9 14 10 10 20

                      in this I wanted the second column to be zero'd out
                      Code:
                      data=open("file").readlines()
                      for num,line in enumerate(data):
                          if num <3 : print line.strip() ; continue
                          newlist=line.split()
                          newlist[1] = '0'
                          print ' '.join(newlist)
                      output:
                      Code:
                      #Number of Bits
                      12
                      #Data
                      0 0 0 0 0 0 0 0 0 0 0 0
                      12 0 3 4 6 4 5 4 7 5 5 10
                      24 0 7 7 13 7 9 9 14 10 10 20

                      Comment

                      • ironmonkey69
                        New Member
                        • Jul 2007
                        • 43

                        #12
                        right now I am using eclipse to write the python code. It uses the root directory of the project when calling a file. Is there as way that I can access a file from a specific directory. On a network through linux?

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #13
                          Originally posted by ironmonkey69
                          right now I am using eclipse to write the python code. It uses the root directory of the project when calling a file. Is there as way that I can access a file from a specific directory. On a network through linux?
                          Have you tried passing the full path in the script? On Windows it would be:
                          r'X:\subdir1\su bdir2\filename. ext'

                          Using os.path:[code=Python]import os
                          filename = os.path.join('X :\\', 'subdir1', 'subdir2', 'filename.ext')[/code]

                          Comment

                          • ironmonkey69
                            New Member
                            • Jul 2007
                            • 43

                            #14
                            This is the code that I have:[CODE=python]
                            import os
                            filename = os.path.join('h ome', 'engine', 'ws', 'test.ext')
                            def nthzero(dataLis t, nth, n):
                            '''
                            Replace the nth element of each list in the data list with 'n'
                            '''
                            for item in dataList:
                            item[nth] = n
                            return dataList

                            fn = 'test.txt'
                            f = open(fn)

                            s = f.next()
                            prefix = s
                            while s.strip() != '#Data':
                            s = f.next()
                            prefix += s

                            lineList = [line.strip().sp lit() for line in f]

                            f.close()
                            elem = 1
                            repl = '0'
                            lineList = nthzero(lineLis t, elem, repl)

                            fn1 = 'test2.txt'
                            f = open(fn1, 'w')
                            outList = []
                            for line in lineList:
                            outList.append( ' '.join(line))

                            f.write('%s%s' % (prefix, '\n'.join(outLi st)))
                            f.close()[/CODE]
                            Last edited by bartonc; Jul 24 '07, 07:56 PM. Reason: Added [CODE=python][/CODE] tags.

                            Comment

                            • ironmonkey69
                              New Member
                              • Jul 2007
                              • 43

                              #15
                              can someone explain to me step by step what is going on?

                              Comment

                              Working...