How do I take in Command Line arguments?

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

    #1

    How do I take in Command Line arguments?

    Right now, I have this python script that lets me choose which column of a text file that I can zero out. Them column nubmer is stored as the variable 'elem'. I also was given a sample of taking in command line arguements. I was wondering if anyone could help me intergrate this into the code so that from the command line I can input a number and have the script zero out the column.

    beginning text file:
    #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

    text file after script has been run:
    #Number of Bits
    12
    #Data
    0 0 0 0 0 0 0 0 0 0 0 0
    0 5 3 4 6 4 5 4 7 5 5 10
    0 9 7 7 13 7 9 9 14 10 10 20
    and this is what it does:
    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.readline()
    prefix = s
    while s.strip() != '#Data':
        s = f.readline()
        prefix += s
        
    lineList = [line.strip().split() for line in f.readlines()]
    for line in f.readlines():
        lineList.append(line.strip().split())
     
    f.close()
    elem = 0
    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()
    example of the command line
    Code:
    /usr/bin/python hello.py \"1, 2, 4, 6, 9\""
    
    hello.py
    import sys
    names = sys.argv[1].split(', ')
    list = []
    for name in names:
       list.append(name)
    print list
    #print "Hello " + sys.argv[1]
    
    output:
    Hello [1, 2, 4, 6, 9]
  • ironmonkey69
    New Member
    • Jul 2007
    • 43

    #2
    This code is in Python 2.0.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      /usr/bin/python hello.py 1 2 4 6 9

      Comment

      • eric dexter
        New Member
        • Sep 2006
        • 46

        #4
        I have a couple of examples that I have been distributing one is in tracker8.py available in my dex tracker project (url at bottom). What I did was use a main function. The code you are writing looks like it would be usefull for what I am doing and maybe something that would allow a series of numbers from a file into the column you are zeroing out might be a good contribution to my project :) ... http://www.stormpages. com/edexter/csound.html

        Comment

        • eric dexter
          New Member
          • Sep 2006
          • 46

          #5
          Originally posted by ironmonkey69
          Right now, I have this python script that lets me choose which column of a text file that I can zero out. Them column nubmer is stored as the variable 'elem'. I also was given a sample of taking in command line arguements. I was wondering if anyone could help me intergrate this into the code so that from the command line I can input a number and have the script zero out the column.

          beginning text file:
          #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

          text file after script has been run:
          #Number of Bits
          12
          #Data
          0 0 0 0 0 0 0 0 0 0 0 0
          0 5 3 4 6 4 5 4 7 5 5 10
          0 9 7 7 13 7 9 9 14 10 10 20
          and this is what it does:
          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.readline()
          prefix = s
          while s.strip() != '#Data':
              s = f.readline()
              prefix += s
              
          lineList = [line.strip().split() for line in f.readlines()]
          for line in f.readlines():
              lineList.append(line.strip().split())
           
          f.close()
          elem = 0
          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()
          example of the command line
          Code:
          /usr/bin/python hello.py \"1, 2, 4, 6, 9\""
          
          hello.py
          import sys
          names = sys.argv[1].split(', ')
          list = []
          for name in names:
             list.append(name)
          print list
          #print "Hello " + sys.argv[1]
          
          output:
          Hello [1, 2, 4, 6, 9]

          I haven't tried this yet but another way to write your program may be to use vim as a com object (with pywin or maybe through wxpython). There was a recent post on the vim board that describes a way to insert a column...

          http://groups.google.c om/group/vim-experiment/browse_thread/thread/447a01d7014be5c 9

          This has all been pretty tempting but I have been coding other stuff

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by ironmonkey69
            Right now, I have this python script that lets me choose which column of a text file that I can zero out. Them column nubmer is stored as the variable 'elem'. I also was given a sample of taking in command line arguements. I was wondering if anyone could help me intergrate this into the code so that from the command line I can input a number and have the script zero out the column.

            beginning text file:
            #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

            text file after script has been run:
            #Number of Bits
            12
            #Data
            0 0 0 0 0 0 0 0 0 0 0 0
            0 5 3 4 6 4 5 4 7 5 5 10
            0 9 7 7 13 7 9 9 14 10 10 20
            and this is what it does:
            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.readline()
            prefix = s
            while s.strip() != '#Data':
                s = f.readline()
                prefix += s
                
            lineList = [line.strip().split() for line in f.readlines()]
            for line in f.readlines():
                lineList.append(line.strip().split())
             
            f.close()
            elem = 0
            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()
            example of the command line
            Code:
            /usr/bin/python hello.py \"1, 2, 4, 6, 9\""
            
            hello.py
            import sys
            names = sys.argv[1].split(', ')
            list = []
            for name in names:
               list.append(name)
            print list
            #print "Hello " + sys.argv[1]
            
            output:
            Hello [1, 2, 4, 6, 9]
            At the command line: python code.py 1 2 4 6 9

            Assign the following to variable 'elem':[code=Python]elem = [int(i) for i in sys.argv[1:]][/code]Following is the code I used:[code=Python]import sys

            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
            except IndexError, e:
            pass
            return dataList

            if __name__ == '__main__':

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

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

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

            f.close()
            #elem = [1,2,3,4,8,9,15]

            # Command Line
            # C:\Python23>pyt hon nthzero.py 1 2 3 4 8 9 15
            elem = [int(i) for i in sys.argv[1:]]

            repl = '0'
            lineList = nthzero(lineLis t, elem, repl)

            fn1 = 'dataout.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

              #7
              this script takes in numbers one at a time eventhough they are listed together. Is there a way that I can input an entire list, i.e. 0 1 2 3 4 5, from the command line and have the script zero them out? Kinda like the example:

              Code:
              import sys
              names = sys.argv[1].split(', ')
              list = []
              for name in names:
                  list.append(name)
              print list
              #print "Hello " + sys.argv[1]

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by ironmonkey69
                this script takes in numbers one at a time eventhough they are listed together. Is there a way that I can input an entire list, i.e. 0 1 2 3 4 5, from the command line and have the script zero them out? Kinda like the example:

                Code:
                import sys
                names = sys.argv[1].split(', ')
                list = []
                for name in names:
                    list.append(name)
                print list
                #print "Hello " + sys.argv[1]
                You can put quotes around your command-line argument: "1, 2, 3" (I think).

                Comment

                • ironmonkey69
                  New Member
                  • Jul 2007
                  • 43

                  #9
                  As I said before, I am a noob at python and am trying to understand what is going on. I was wondering if you help me understand what a couple of the lines of the script are doing.

                  Code:
                   lineList = [line.strip().split() for line in f.readlines()]

                  Comment

                  • ironmonkey69
                    New Member
                    • Jul 2007
                    • 43

                    #10
                    how does this script work?

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by ironmonkey69
                      As I said before, I am a noob at python and am trying to understand what is going on. I was wondering if you help me understand what a couple of the lines of the script are doing.

                      Code:
                       lineList = [line.strip().split() for line in f.readlines()]
                      That's called a list comprehension. In order to understand it, let's break it down:[CODE=python]tempList = f.readlines()
                      lineList = []
                      for line in tempList:
                      newLine = line.strip()
                      print newLine
                      wordList = newLine.split()
                      print wordList
                      lineList.append (wordList)
                      print lineList[/CODE]Does that help?

                      Comment

                      • ironmonkey69
                        New Member
                        • Jul 2007
                        • 43

                        #12
                        it helps a little more. Can you add some comments to the code to help me get a better a better understanding of which sections do what?

                        Comment

                        Working...