This problem really asks for the use of a dictionary object ...
# typical test data
data = """\
1 20
2 56
3 42
4 53
5 90
6 33
"""
fname = 'mytest123.txt'
# write test data to a file
fout = open(fname, "w")
fout.write(data)
fout.close()
# read the test file and convert to user_val:value dictionary
user_dict = {}
for line in open(fname):
# remove trailing newline char
line = line.rstrip()
key, val = line.split()
user_dict[key] = val
print user_dict # test
"""result -->
{'1': '20', '3': '42', '2': '56', '5': '90', '4': '53', '6': '33'}
"""
# assume user input from the commandline is ...
user_input = '1,2,3'
for user_val in user_input.split(','):
print user_val, user_dict[user_val]
"""result -->
1 20
2 56
3 42
"""
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
Offline 5,792 posts
since Oct 2004