So I wrote a function that reads in a .csv file and creates a dictionary of abbreviations and their meaning. Now I'm looking to create a main function that calls the create dictionary function and then prompts the user for keys, but I'm not sure how to access the created dictionary.
import csv
def CreateDictionary(fileName):
with open(fileName, 'r') as f:
reader = csv.reader(f)
newDict = {}
for x, y in reader:
newDict.setdefault(x, []).append(y)
return newDict
def main():
CreateDictionary('textToEnglish.csv')
key = input("Please enter a text abbreviation")
for key, value in newDict:
You need to assign the return value of the CreateDictionary
function to some variable like this:
newDict=CreateDictionary('textToEnglish.csv')
this way you can access the items in the dictionary like this:
newDict["etc"]
You have to store the return value of CreateDictionary
into a variable.
myDict = CreateDictionary('textToEnglish.csv')
By the way: You should rename the variable key
because it will be overwritten in the for
loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.