By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,285 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,285 IT Pros & Developers. It's quick & easy.

Help with command line arguements

ironmonkey69
P: 43
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:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. def nthzero(dataList, nth, n):
  3.     '''
  4.     Replace the nth element of each list in the data list with 'n'
  5.     '''
  6.     for item in dataList:
  7.         item[nth] = n
  8.     return dataList
  9.  
  10.  
  11. fn = 'outfile.txt'
  12. f = open(fn)
  13.  
  14. s = f.next()
  15. prefix = s
  16. while s.strip() != '#Data':
  17.     s = f.next()
  18.     prefix += s
  19.  
  20. lineList = [line.strip().split() for line in f]
  21.  
  22. f.close()
  23. elem = 0
  24. repl = '0'
  25. lineList = nthzero(lineList, elem, repl)
  26.  
  27. fn1 = 'outfile.txt'
  28. f = open(fn1, 'w')
  29. outList = []
  30. for line in lineList:
  31.     outList.append(' '.join(line))
  32.  
  33. f.write('%s%s' % (prefix, '\n'.join(outList)))
  34. f.close()
  35. ...
Aug 3 '07 #1
Share this Question
Share on Google+
2 Replies


bartonc
Expert 5K+
P: 6,596
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:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. def nthzero(dataList, nth, n):
  3.     '''
  4.     Replace the nth element of each list in the data list with 'n'
  5.     '''
  6.     for item in dataList:
  7.         item[nth] = n
  8.     return dataList
  9.  
  10.  
  11. fn = 'outfile.txt'
  12. f = open(fn)
  13.  
  14. s = f.next()
  15. prefix = s
  16. while s.strip() != '#Data':
  17.     s = f.next()
  18.     prefix += s
  19.  
  20. lineList = [line.strip().split() for line in f]
  21.  
  22. f.close()
  23. elem = 0
  24. repl = '0'
  25. lineList = nthzero(lineList, elem, repl)
  26.  
  27. fn1 = 'outfile.txt'
  28. f = open(fn1, 'w')
  29. outList = []
  30. for line in lineList:
  31.     outList.append(' '.join(line))
  32.  
  33. f.write('%s%s' % (prefix, '\n'.join(outList)))
  34. f.close()
  35. ...
It looks like your program may have some logic error around here:
Expand|Select|Wrap|Line Numbers
  1. lineList = [line.strip().split() for line in f]
but here's how you go about getting the command-line args:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. print sys.argv[0]  # Always contains the name of the file being executed.
  3. print sys.argv[1:] # Use a slice to avoid index errors.
Hope that helps.
Aug 3 '07 #2

ironmonkey69
P: 43
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?
Aug 3 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.