Reputation: 909
I am very new to python and basically trying to send a list argument and print it using python
import unittest
import sys
class TestStringMethods():
def test_upper(self, input_list):
print input_list
if __name__ == '__main__':
input_list = sys.argv[1]
test_upper(input_list)
How should i give the input from the command line? I tried this
python test.py 1,2,3
python test.py test_upper: input_list=[1,2,3]
python test.py TestStringMethods.test_upper: input_list=[1,2,3]
nothing worked
Upvotes: 1
Views: 339
Reputation: 5519
jonrsharpe has already specified this method in comments.
This would be the easiest way to achieve what you want.
You can a give the input numbers separated by spaces as command line arguments.
This can avoid the use of split
function
Code
import sys
input_list = sys.argv[1:]
print (input_list)
Output
python3 script.py 1 2 3 4 5
['1', '2', '3', '4', '5']
Modifying your code
import unittest
import sys
class TestStringMethods:
def __init__(self):
pass
def test_upper(self, input_list):
print (input_list)
input_list = sys.argv[1:]
c=TestStringMethods()
c.test_upper(input_list)
Output
python3 script.py 1 2 3 4 5
['1', '2', '3', '4', '5']
Upvotes: 1
Reputation: 1418
This should work!!
if __name__ == '__main__':
string_input = sys.argv[1]
input_list = string_input.split(",") #splits the input string on spaces and comma
# process string elements in the list and make them integers
input_list = [int(a) for a in input_list]
test_upper(input_list)
Just to test if the idea works.
Run the following code in idle or any IDE that you are using. I assume you are using python 2.7
string_input = "1, 2, 3"
input_list = string_input.split(",") #splits the input string on spaces and comma
# process string elements in the list and make them integers
input_list = [int(a) for a in input_list]
print input_list
Upvotes: 0