简体   繁体   English

如何仅从python词典中获取一个单词?

[英]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.

相关问题 如何从网站/词典中获取单个单词? - How do I get a single word from a website/dictionary? 如何只为一个 Key 更新 Set in Dictionary? (Python) - How do I update Set in Dictionary for only one Key? (Python) 仅从 python 中的字典中获取一个单词 - Get only one word from a dictonary in python 如何从 python 的字典中获取数据? - How do I get data from dictionary in python? 更新python中的字典,如何从用户那里获取输入? - Update dictionary in python, how do I get input from user? 我如何让我的字典从我的正则表达式循环中打印出更多的值,而不是只打印最后一个值? - How do I get my dictionary to print out more values from my regex loop instead of printing only the last one? Python-如何通过空格将标点符号与单词分开,在标点符号和单词之间仅留一个空格? - Python - How do I separate punctuation from words by white space leaving only one space between the punctuation and the word? 如何仅从 python 中的字符串中获取单词? - How to get only the word from a string in python? 如何让这个 Python 代码只输出一行? - How do I get this Python code to output only one line? 如何仅使用 for 循环从字典中一一获取键? - How do I only obtain keys from a dictionary one by one using a for loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM