0

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.

  • Does said separate file contain anything but the dictionary? Have you considered using common data formats such as JSON instead? – MisterMiyagi May 06 '19 at 14:13
  • @MisterMiyagi No. It contains nothing but the created dictionaries. And no, I have not tried any formats like JSON. I have no experience with JSON, but would be open to learning it. Thanks. – AndyMaratea May 06 '19 at 16:17

1 Answers1

0

You can just import the dictionary you defined in file.py, so if the file.py looks like

file.py

dct = {....}

Then you code will change to

#Import dct from file
from file import dct

def searchFunction():

    dictSearch = input("Enter name of donor (MAKE NAME ONE WORD WITH FIRST LETTER CAPS! ex. \"SomeText\"): ")

    #Perform your search
    if dictSearch in dct:
        print(dictSearch)

    else:
        print("False")
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • What is "dct" representative of in your example? I see what is being done, but do not know what dct will correspond to in my code. Thanks. – AndyMaratea May 06 '19 at 16:53
  • dct is the dictionary in another file, as you said in the question return the dictionary and its contents like it is shown in the other file (file.py) @AndyMaratea :) – Devesh Kumar Singh May 07 '19 at 11:07
  • Thanks for the help. That worked for individual dictionaries. The problem I am stuck on now is that the problem calls for me to create multiple dictionaries with unique names, which are generated by a series of inputs and stored in a separate file (file.py). The variable dictSearch is supposed to return the dictionary with the unique name assigned to dictSearch. I cannot seem to get that part to work. @Devesh Kumar Singh – AndyMaratea May 07 '19 at 13:29
  • Could I please how file.py would look like with these dictionary names and how do you like to search as an update on the question please @AndyMaratea – Devesh Kumar Singh May 07 '19 at 19:25
  • Thanks, I have updated the code in the question to show how I create the unique dictionaries, as well as how file.py would look. Hopefully that can help you out. @Devesh Kumar Singh – AndyMaratea May 08 '19 at 13:14