473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How do I take in Command Line arguments?

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:
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
11 2708
This code is in Python 2.0.
Aug 6 '07 #2
bartonc
6,596 Expert 4TB
/usr/bin/python hello.py 1 2 4 6 9
Aug 6 '07 #3
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
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
2,851 Expert Mod 2GB
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
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
6,596 Expert 4TB
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
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
how does this script work?
Aug 15 '07 #10
bartonc
6,596 Expert 4TB
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
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

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

Similar topics

6
by: Hari | last post by:
can i have command line arguments in VS.NET applicatio? if yes how? Can i have some code snippets of the above functionality? I know we can acjieve this in console application form command...
2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
1
by: amirmira | last post by:
I would like to set command line arguments to a service at install time. I need to do this because I need to get information from different registry locations depending on my command line argument....
6
by: Hari | last post by:
can i have command line arguments in VS.NET applicatio? if yes how? Can i have some code snippets of the above functionality? I know we can acjieve this in console application form command...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.