2017-04-23 28 views
0

我試圖說明翻譯字典即:如何使一個字典,對於關鍵是單詞在英語和值希臘字(UTF-8),我得到一個KeyError,當我嘗試獲得值

  • 當一個英文單詞給出我回到含義希臘(UTF-8)

    #Possible Content Of File: 
    #"Hello,Γεια\n" 
    #"Car,Αμαξι\n" 
    
    with codecs.open("dict.txt",'r',encoding='utf8') as f: 
        lines= f.read() #contains all the lines of the file 
    
    word=raw_input("Word To Find\n") 
    flag=0 
    temp="" 
    temp2="" 
    dictionary={} 
    #here is an algorithm i came up with, to seperate the string and get on temp the key 
    #and on temp2 the value , then i add them on the dictionary 
    for i in range(0,len(lines)): 
        if flag==0: 
         temp+=lines[i] 
        if lines[i]==',': 
         flag=1 
         continue 
        if lines[i]=='\n': 
         flag=0 
         dictionary.update({temp:temp2}) #adding the key and the value 
         #(the value is in utf-8) 
         temp="" 
         temp2="" 
        if flag==1: 
         temp2+=lines[i] 
    
    #print(dictionary.keys()) # [u'Hello,',u'Car,'] 
    #print(dictionary.get(word)) returns None 
    print(dictionary[word])# KeyError 
    

Note: The file contains this kind of strings: "Hello,Γεια\n" and "Car,Αμαξι\n"

Error: print(dictionary[word]) throws a KeyError

This is Python 2.7

感謝很多提前

+1

調試步驟1:'print(dictionary.keys())'。調試完成。修復代碼的步驟1:'dic_file = open('dict.txt'); dictionary = {e:g.strip()for e,g in(line.split(',')for line in dic_file)}'。 –

+0

我收到相同的內容11次,如果我打印字典 – Phill

回答

0

該問題的實現需要兩個循環。外環穿過線,通過當前行的字符內循環,例如:

for line in lines: 
    for i in range(0, len(line)): 
     # Then using line[i] instead of lines[i] 

另外,read()獲得了整個內容,readlines()是正確的功能。

以下實施方式使用split(',')來分隔一行中的單詞。 另外,字典可以通過簡單的dictionary[key] = value進行更新。 進口編碼解碼器

with codecs.open("dict.txt", 'r', encoding='utf8') as f: 
    lines = f.readlines() 

word=raw_input("Word To Find\n") 
dictionary={} 
for line in lines: 
    if ',' in line: 
     key, value = line.split(',') 
     dictionary[key.strip()] = value.strip() 
    else: 
     print('Cannot parse line: ' + line) 

if word in dictionary: 
    print(dictionary[word]) 
else: 
    print('Error: Translation not found.') 

以前的實現假定該值沒有逗號。 以下版本允許逗號值:

for line in lines: 
    comma_index = line.index(',') 
    if comma_index >= 0: 
     key = line[0:comma_index] 
     value = line[comma_index + 1:] 
     dictionary[key.strip()] = value.strip() 
    else: 
     print('Cannot parse line: ' + line) 
+0

OP命名變量「行」,但它實際上不是行列表;它是一個包含文件全部內容的字符串。所以不,這不是問題。 –

+0

爲什麼你不通過行自己迭代:'對於c行:'? –

+0

@YevhenKuzmovych如果不需要,我不會遍歷字符。更新後的答案使用簡單的'split(',')'。 –

0

你的分裂算法有點越野車,並且有內置支持做在python相同的任務,你爲什麼要推倒重來?

import io 
with io.open("test.txt",'r',encoding='utf8') as f: 
     list_of_lines= f.read().splitlines() #contains all the lines of the file in a list 


dictionary={} 

for line in list_of_lines: 
    eng, greek = line.split(',') # split(',') will split the string by ',' and make a list out of it 
    dictionary[eng] = greek 

print(dictionary['Hello']) 
+0

由於某種原因,我得到一個TypeError:'編碼'是這個函數無效的關鍵字爭論 行:與開放(「dict.txt」,'r',編碼='utf8')爲f: – Phill

+0

這是因爲你正在使用python2,這是python3的解決方案。讓我稍後發佈修復。 –

+0

@Phill,答案已更新,並將在python2中工作。 –

相關問題