I am creating a dictionary which is added into a separate file. I am trying to use an input (dictSearch) to try and return the dictionary and its contents like it is shown in the other file (file.py). The problem is that the program is searching through the second file for a string called "dictSearch", not for what dictSearch is assigned to.
I have tried to change the data type of dictSearch.
def searchFunction():
with open('file.py') as myfile:
dictSearch = input("Enter name of donor (MAKE NAME ONE WORD WITH FIRST LETTER CAPS! ex. \"SomeText\"): ")
if dictSearch in myfile.read():
print(dictSearch)
else:
print("False")
#Below is what file.py looks like.
Andy = {
'age':'18',
'bloodType':'O-',
'diseases':'NA',
'medications':'NA',
'name':'Andy',
}
MATT = {
'age':'21',
'bloodType':'A',
'diseases':'NA',
'medications':'NA',
'name':'MATT',
}
Ex2 = {
'age':'1354',
'bloodType':'B',
'diseases':'NA',
'medications':'NA',
'name':'Ex2',
}
#Below is how I am creating these unique dictionaries in a separate file (in file.py)
with open('file.py','a') as file: #Code from lines x-y gotten from https://stackoverflow.com/questions/36965507/writing-a-dictionary-to-a-text-file
file.write(str(dictName) + " = { \n")
for k in sorted (donorDictionary.keys()):
file.write("'%s':'%s', \n" % (k, donorDictionary[k]))
file.write("} ")
file.write("\n")
file.close()
I expect the output to return the input dictionary and its key/value pairs, but it is returning the string that dictSearch is assigned to.