There are many questions similar to this out there but none of the answers solved my issue.
I have defined several functions that parse large data sets. First I call the data, then I organize the data (represented as rows and columns in a .txt) into lists which I will index for individual data entries. After that I establish my functions that will work through the lists one at a time. The code looks like:
f = open(fn)
for line in iter(f):
entries = [i for i in line.split() if i]
def function_one():
if entries[0] == 150:
# do something
def function_two():
if entries[1] == 120:
# do something else
def function_three():
if len(entries) > 10:
# do something else
etc. etc.
I have attempted to prompt the user asking what function they would like to execute as each function returns different things about the data set. My attempt is as follows:
f_call = input('Enter Function Name: ')
if f_call in locals().keys() and callable(locals()['f_call']):
locals()['f_call']()
else:
print('Function Does Not Exist')
When I run the script, I am prompted to 'Enter Function Name:'
and if I type in 'function_one'
and return, it prints
'Function Does Not Exist'
. I want to see that, if entered correctly, the script will execute only the function that the user entered. If the user input is correct, the function should run and print
the parsed data.
I have also attempted using a dict
to store the functions but I have not had success.
Any help would be greatly appreciated.