english_list = ["fire","apple","morning","river","wind"]
spanish_list = ["fuego","manzana","mañana","río","viento"]
english_to_spanish = dict(zip(english_list, spanish_list))
spanish_to_english = dict(zip(spanish_list, english_list))
def translate(word):
translation = english_to_spanish.get(word)
if translation:
return translation
translation = spanish_to_english.get(word)
if translation:
return translation
raise Exception('Word {0} does not exists'.format(word))
print("Welcome to the English <--> Spanish Dictionary")
while True:
word = input("> ")
if word == 'show':
wordlist = input("Would you like to see the English or Spanish wordlist?")
if wordlist == 'english':
print(english_list)
elif wordlist == 'spanish':
print(spanish_list)
else:
try:
print(translate(word))
except Exception as exeception:
print ("That wasn't a option")
这是我的代码,我想导入一个字典(我有),但我不知道该怎么做,我是新编码,真的需要一些帮助,这是一个学校作业,我真的需要熟练帮助,任何帮助都会受到大力赞赏!!
答案 0 :(得分:0)
如果要从json文件导入字典,可以这样做
import json
json_path = 'your_json_file.json'
with open(json_path) as json_data:
your_dictionary = json.load(json_data)