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

How do I take in Command Line arguments?

ironmonkey69
P: 43
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:
Expand|Select|Wrap|Line Numbers
  1. def nthzero(dataList, nth, n):
  2.     '''
  3.     Replace the nth element of each list in the data list with 'n'
  4.     '''
  5.     for item in dataList:
  6.         item[nth] = n
  7.     return dataList
  8.  
  9. fn = '/outfile.txt'
  10. f = open(fn)
  11.  
  12. s = f.readline()
  13. prefix = s
  14. while s.strip() != '#Data':
  15.     s = f.readline()
  16.     prefix += s
  17.  
  18. lineList = [line.strip().split() for line in f.readlines()]
  19. for line in f.readlines():
  20.     lineList.append(line.strip().split())
  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.  
example of the command line
Expand|Select|Wrap|Line Numbers
  1. /usr/bin/python hello.py \"1, 2, 4, 6, 9\""
  2.  
  3. hello.py
  4. import sys
  5. names = sys.argv[1].split(', ')
  6. list = []
  7. for name in names:
  8.    list.append(name)
  9. print list
  10. #print "Hello " + sys.argv[1]
  11.  
  12. output:
  13. Hello [1, 2, 4, 6, 9]
  14.  
Aug 6 '07 #1
Share this Question
Share on Google+
11 Replies


ironmonkey69
P: 43
This code is in Python 2.0.
Aug 6 '07 #2

bartonc
Expert 5K+
P: 6,596
/usr/bin/python hello.py 1 2 4 6 9
Aug 6 '07 #3

P: 46
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
Aug 7 '07 #4

P: 46
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:
Expand|Select|Wrap|Line Numbers
  1. def nthzero(dataList, nth, n):
  2.     '''
  3.     Replace the nth element of each list in the data list with 'n'
  4.     '''
  5.     for item in dataList:
  6.         item[nth] = n
  7.     return dataList
  8.  
  9. fn = '/outfile.txt'
  10. f = open(fn)
  11.  
  12. s = f.readline()
  13. prefix = s
  14. while s.strip() != '#Data':
  15.     s = f.readline()
  16.     prefix += s
  17.  
  18. lineList = [line.strip().split() for line in f.readlines()]
  19. for line in f.readlines():
  20.     lineList.append(line.strip().split())
  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.  
example of the command line
Expand|Select|Wrap|Line Numbers
  1. /usr/bin/python hello.py \"1, 2, 4, 6, 9\""
  2.  
  3. hello.py
  4. import sys
  5. names = sys.argv[1].split(', ')
  6. list = []
  7. for name in names:
  8.    list.append(name)
  9. print list
  10. #print "Hello " + sys.argv[1]
  11.  
  12. output:
  13. Hello [1, 2, 4, 6, 9]
  14.  

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.com/group/vim-experiment/browse_thread/thread/447a01d7014be5c9

This has all been pretty tempting but I have been coding other stuff
Aug 7 '07 #5

bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. def nthzero(dataList, nth, n):
  2.     '''
  3.     Replace the nth element of each list in the data list with 'n'
  4.     '''
  5.     for item in dataList:
  6.         item[nth] = n
  7.     return dataList
  8.  
  9. fn = '/outfile.txt'
  10. f = open(fn)
  11.  
  12. s = f.readline()
  13. prefix = s
  14. while s.strip() != '#Data':
  15.     s = f.readline()
  16.     prefix += s
  17.  
  18. lineList = [line.strip().split() for line in f.readlines()]
  19. for line in f.readlines():
  20.     lineList.append(line.strip().split())
  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.  
example of the command line
Expand|Select|Wrap|Line Numbers
  1. /usr/bin/python hello.py \"1, 2, 4, 6, 9\""
  2.  
  3. hello.py
  4. import sys
  5. names = sys.argv[1].split(', ')
  6. list = []
  7. for name in names:
  8.    list.append(name)
  9. print list
  10. #print "Hello " + sys.argv[1]
  11.  
  12. output:
  13. Hello [1, 2, 4, 6, 9]
  14.  
At the command line: python code.py 1 2 4 6 9

Assign the following to variable 'elem':
Expand|Select|Wrap|Line Numbers
  1. elem = [int(i) for i in sys.argv[1:]]
Following is the code I used:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. def nthzero(dataList, nthList, n):
  4.     '''
  5.     Replace the nth element of each list in dataList with 'n'
  6.     '''
  7.     for nth in nthList:
  8.         for item in dataList:
  9.             try:
  10.                 item[nth] = n
  11.             except IndexError, e:
  12.                 pass
  13.     return dataList
  14.  
  15. if __name__ == '__main__':
  16.  
  17.     fn = 'datain.txt' 
  18.     f = open(fn) 
  19.  
  20.     s = f.readline()
  21.     prefix = s
  22.     while s.strip() != '#Data':
  23.         s = f.readline()
  24.         prefix += s
  25.  
  26.     lineList = [line.strip().split() for line in f.readlines()] 
  27.  
  28.     f.close() 
  29.     #elem = [1,2,3,4,8,9,15]
  30.  
  31.     # Command Line
  32.     # C:\Python23>python nthzero.py 1 2 3 4 8 9 15
  33.     elem = [int(i) for i in sys.argv[1:]]
  34.  
  35.     repl = '0' 
  36.     lineList = nthzero(lineList, elem, repl) 
  37.  
  38.     fn1 = 'dataout.txt' 
  39.     f = open(fn1, 'w')
  40.  
  41.     outList = []
  42.  
  43.     for line in lineList:
  44.         outList.append(' '.join(line))
  45.  
  46.     f.write('%s%s' % (prefix, '\n'.join(outList)))
  47.     f.close()
Aug 7 '07 #6

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

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. names = sys.argv[1].split(', ')
  3. list = []
  4. for name in names:
  5.     list.append(name)
  6. print list
  7. #print "Hello " + sys.argv[1]
  8.  
Aug 14 '07 #7

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

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. names = sys.argv[1].split(', ')
  3. list = []
  4. for name in names:
  5.     list.append(name)
  6. print list
  7. #print "Hello " + sys.argv[1]
  8.  
You can put quotes around your command-line argument: "1, 2, 3" (I think).
Aug 14 '07 #8

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

Expand|Select|Wrap|Line Numbers
  1.  lineList = [line.strip().split() for line in f.readlines()]
  2.  
Aug 15 '07 #9

ironmonkey69
P: 43
how does this script work?
Aug 15 '07 #10

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

Expand|Select|Wrap|Line Numbers
  1.  lineList = [line.strip().split() for line in f.readlines()]
  2.  
That's called a list comprehension. In order to understand it, let's break it down:
Expand|Select|Wrap|Line Numbers
  1. tempList = f.readlines()
  2. lineList = []
  3. for line in tempList:
  4.     newLine = line.strip()
  5.     print newLine
  6.     wordList = newLine.split()
  7.     print wordList
  8.     lineList.append(wordList)
  9. print lineList
Does that help?
Aug 15 '07 #11

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

Post your reply

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