So I am creating a dictionary with the values given in a csv file and now I am trying to have an input where you type in a key and it will check the dictionary for that key and then return the value. I am having trouble implementing this but this is what I have and I believe I should be using d.get() but I’m not 100 percent sure.
import csv
dictionary = []
line = 0
reader = csv.reader(open("all.csv", "rb"), delimiter = ",")
header = reader.next()
for column in reader:
line = line + 1
dictionary.append({column[0]:column[2]})
print dictionary
check = raw_input("Enter word in dictionary to get its value: ")
print dictionary.get(check, "This word doesnt exist in the dictionary")
That is not a dictionary, it is a list. Thus, it doesn’t have a
getmethod.What you want to do is initialize the dictionary like this:
(Note the curly braces rather than the square brackets). Also change the assignment line to this:
At that point, your program should work.