running a python script with tcl

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

    running a python script with tcl

    I am trying to run a script though a tcl ware command and cannot get it to run. I used to eclipse to write the script and was able to run it through eclipse.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by ironmonkey69
    I am trying to run a script though a tcl ware command and cannot get it to run. I used to eclipse to write the script and was able to run it through eclipse.
    Help us help you by posting some of the relevant code. Instructions for using [ CODE ] tags are on the right hand side of the page when you reply to this. Thanks.

    Comment

    • ironmonkey69
      New Member
      • Jul 2007
      • 43

      #3
      actually I figured out the problem. I am running python 2.5 when the code that I am writing is for a machine that uses python 2.0. Can you help me do the same with this code in python 2.0?

      Code:
      ...
      def nthzero(dataList, 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 = 'outfile.db'
      f = open(fn)
       
      s = f.next()
      prefix = s
      while s.strip() != '#Data':
          s = f.next()
          prefix += s
          
      lineList = [line.strip().split() for line in f]
       
      f.close()
      elem = 1
      repl = '0'
      lineList = nthzero(lineList, elem, repl)
      
      fn1 = 'outfile.db'
      f = open(fn1, 'w')
      outList = []
      for line in lineList:
          outList.append(' '.join(line))
          
      f.write('%s%s' % (prefix, '\n'.join(outList)))
      f.close()
      ...

      Comment

      • ironmonkey69
        New Member
        • Jul 2007
        • 43

        #4
        this code takes a text file with numbers that are separated by columns and zeroes out a colum. this is what the text 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

        and this is what it does:

        #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 the python code the 'elem=' statement is what chooses the column

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by ironmonkey69
          actually I figured out the problem. I am running python 2.5 when the code that I am writing is for a machine that uses python 2.0. Can you help me do the same with this code in python 2.0?

          Code:
          ...
          def nthzero(dataList, 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 = 'outfile.db'
          f = open(fn)
           
          s = f.next()
          prefix = s
          while s.strip() != '#Data':
              s = f.next()
              prefix += s
              
          lineList = [line.strip().split() for line in f]
           
          f.close()
          elem = 1
          repl = '0'
          lineList = nthzero(lineList, elem, repl)
          
          fn1 = 'outfile.db'
          f = open(fn1, 'w')
          outList = []
          for line in lineList:
              outList.append(' '.join(line))
              
          f.write('%s%s' % (prefix, '\n'.join(outList)))
          f.close()
          ...
          Initially I thought the list comprehension or string methods may fail in Python 2.0, but I believe both were added in 2.0. Can you post the error message?

          Comment

          • ironmonkey69
            New Member
            • Jul 2007
            • 43

            #6
            I'm calling the file 'monkey2.py. This is the error I have been getting:

            File "monkey2.py ", line 11, in ?
            f = open(fn)
            IOError: [Errno 2] No such file or directory: 'outfile.txt's = f.KEY_NEXT
            AttributeError: 'file' object has no attribute 'KEY_NEXT'

            This is the code I have been playing with:

            Code:
            ...
            def nthzero(dataList, 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 = 'outfile.txt'
            f = open(fn)
             
            s = f.next()
            prefix = s
            while s.strip() != '#Data':
                s = f.next()
                prefix += s
                
            lineList = [line.strip().split() for line in f]
             
            f.close()
            elem = 1
            repl = '0'
            lineList = nthzero(lineList, 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(outList)))
            f.close()
            ...
            I was playing with the file with a .db extension since I can't use the 'next()' in 2.0

            Comment

            • ironmonkey69
              New Member
              • Jul 2007
              • 43

              #7
              actually that's the wrong error. I was getting attributrerror: next()

              Comment

              • ironmonkey69
                New Member
                • Jul 2007
                • 43

                #8
                Here is the error

                Traceback (most recent call last):
                File "monkey.py" , line 13, in ?
                s = f.next()
                AttributeError: next

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Originally posted by ironmonkey69
                  actually that's the wrong error. I was getting attributrerror: next()
                  I just checked. The file.next() method was added in 2.3. Try replacing that section of code with:[code=Python]s = f.readline()
                  prefix = s
                  while s.strip() != '#Data':
                  s = f.readline()
                  prefix += s[/code]

                  Comment

                  • ironmonkey69
                    New Member
                    • Jul 2007
                    • 43

                    #10
                    now I am getting an error:
                    File "monkey.py" , line 19, in ?
                    lineList = [line.strip().sp lit() for line in f]
                    TypeError: loop over non-sequence

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      Originally posted by ironmonkey69
                      now I am getting an error:
                      File "monkey.py" , line 19, in ?
                      lineList = [line.strip().sp lit() for line in f]
                      TypeError: loop over non-sequence
                      Let's get rid of the comp:[code=Python]lineList = []
                      for line in f:
                      lineList.append (line.strip().s plit())[/code]

                      Comment

                      • ironmonkey69
                        New Member
                        • Jul 2007
                        • 43

                        #12
                        Now I am getting this error:

                        Traceback (most recent call last):
                        File "monkey.py" , line 20, in ?
                        for line in f:
                        TypeError: loop over non-sequence

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #13
                          Originally posted by ironmonkey69
                          Now I am getting this error:

                          Traceback (most recent call last):
                          File "monkey.py" , line 20, in ?
                          for line in f:
                          TypeError: loop over non-sequence
                          ??[code=Python]lineList = [line.strip().sp lit() for line in f.readlines()][/code]Do you understand what is taking place here? Since there was no next() method in Python 2.0, this was the way to iterate on a file (I am concluding this by deduction).

                          Comment

                          • ironmonkey69
                            New Member
                            • Jul 2007
                            • 43

                            #14
                            I'm still getting the sam error:

                            Traceback (most recent call last):
                            File "testing.py ", line 20, in ?
                            for line in f:
                            TypeError: loop over non-sequence

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #15
                              Originally posted by ironmonkey69
                              I'm still getting the sam error:

                              Traceback (most recent call last):
                              File "testing.py ", line 20, in ?
                              for line in f:
                              TypeError: loop over non-sequence
                              Please read my last post!

                              for line in f.readlines():

                              Comment

                              Working...