user3281086
user3281086

Reputation: 15

How to to read tuple dynamically named in command line argument (sys.argv)?

How to to read tuple dynamically named in command line argument (sys.argv) ?

Below example doesn’t work.

#!/usr/bin/env python
import sys
cmd1 = sys.argv[1]
tpl1 = ('one', 'two', 'three')
tpl2 = ('four', 'five', 'six')
print (cmd1)
val1 = cmd1[0]
val2 = cmd1[1]
val3 = cmd1[2]
print (val1)
print (val2)
print (val3)

current output for script.py tpl1 :

t
p
l

desired output for script.py tpl1 :

one
two
three

Please advise, thanks

Upvotes: 0

Views: 213

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250931

Use a dictionary, accessing variables using strings is almost never a good idea.

d = {
      'tpl1': ('one', 'two', 'three'),
      'tpl2': ('four', 'five', 'six')
    }

cmd1 = sys.argv[1]
print (d[cmd1])

Upvotes: 3

Related Questions