2017-08-03 71 views
0
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") 

這是我的代碼,我想導入一個字典(我有),但我不知道如何做到這一點,我是相當新的編碼,真的需要一些幫助,這是一個學校的任務,我真的需要無縫的幫助,任何幫助將大量讚賞!導入字典

+1

什麼是要導入的詞典的*格式*? –

+0

通過導入,你的意思是說你有一個帶有單詞對的文件(英文與西班牙文配對)?或者如你的例子所暗示的,你有兩個單獨的文件,包含英文單詞和西班牙文單詞列表? –

回答

0

如果你想導入你的字典從JSON文件,你可以這樣做

import json 

json_path = 'your_json_file.json' 
with open(json_path) as json_data: 
    your_dictionary = json.load(json_data)