我怎么只从python中的字典中获取一个单词?

时间:2017-02-07 01:13:47

标签: python dictionary

我正在python中写一本英语到西班牙语的字典。我有用户输入他们想要翻译的内容,我的程序读取我创建的文件并打印西班牙语等同于该单词。目前它只打印整本字典。这可能很容易,但我是新手,似乎无法弄明白。谢谢

`# coding: utf-8
s=(input("please enter a word to be translated: "))
file=open("dictionary.txt")
engtospa={}
for s in file:
    aList=s.split()
    b=aList[0:]
    engtospa[s]=aList[1:0]
print(engtospa)

`  

1 个答案:

答案 0 :(得分:1)

您需要指定要翻译的密钥。

# coding: utf-8
s=(input("please enter a word to be translated: "))
file=open("dictionary.txt")
engtospa={}
for s in file:
    aList=s.split()
    b=aList[0:]
    engtospa[s]=aList[1:0]
print(engtospa) # <-- your code 
print(engtospa[s]) # <-- expected