Help with command line arguements

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

    #1

    Help with command line arguements

    I need help being able to pass a number from a command line into a script that zeroes out columns of a text file. Using python 2.0.

    Here 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

    Here is what the Python 2.0 code looks like:

    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 = 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()
    ...
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by ironmonkey69
    I need help being able to pass a number from a command line into a script that zeroes out columns of a text file. Using python 2.0.

    Here 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

    Here is what the Python 2.0 code looks like:

    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 = 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()
    ...
    It looks like your program may have some logic error around here:
    Code:
    lineList = [line.strip().split() for line in f]
    but here's how you go about getting the command-line args:[CODE=python]import sys
    print sys.argv[0] # Always contains the name of the file being executed.
    print sys.argv[1:] # Use a slice to avoid index errors.[/CODE]Hope that helps.

    Comment

    • ironmonkey69
      New Member
      • Jul 2007
      • 43

      #3
      now that I can get the command line arguemet into the python script, can you help me get more than one variable into the 'elem=' lin to accept more than one number to zero out more than one columns?

      Comment

      Working...