[英]How do I only get one word from a dictionary in python?
I'm writing an English to Spanish dictionary in python. 我正在用python写英语到西班牙语字典。 I have the user input what they want translated and the my program reads a file I created and print the Spanish equivalent of the word. 我让用户输入他们想要翻译的内容,然后我的程序读取我创建的文件并打印该单词的西班牙语等效词。 Currently it just print the whole dictionary. 目前,它仅打印整个词典。 This is probably easy but I am new to this and can't seem to figure it out. 这可能很容易,但是我对此并不陌生,似乎无法弄清楚。 Thanks 谢谢
`# 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)
`
You need to specify the key which you want to translate. 您需要指定要翻译的密钥。
# 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.