2017-02-07 5 views
0

저는 파이썬으로 스페인어 사전에 영어를 씁니다. 나는 그들이 번역하고자하는 것을 사용자가 입력하고 내 프로그램은 내가 만든 파일을 읽고 그 단어에 해당하는 스페인어를 인쇄한다. 현재는 전체 사전을 인쇄합니다. 이것은 아마 쉽지만 나는 이것에 익숙하지 않으며 그것을 이해하는 것처럼 보일 수 없다. 감사합니다사전에서 파이썬으로 한 단어 만 가져 오는 방법은 무엇입니까?

`# 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

'engtospa [ 'word']'? –

+5

사전 인덱싱은 매우 기본적인 개념이며 스택 오버플로에 대한 주제와 관련이 없습니다. [사전에 대한 문서] (https://docs.python.org/3/tutorial/datastructures.html#dictionaries)를 살펴 보시기 바랍니다. –

+0

또한 일반 텍스트 파일 대신'json'을 사용하는 것도 좋습니다. –

답변

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 
관련 문제