I tried to use difflib to compare words and sentences (in this case something like dictionary) and when I try to compare difflib output with keys in dictionary I get KeyError. Can anyone explain to me why this happens? When I'm not using difflib everything works fine.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import difflib
import operator
lst = ['król']
word = 'król'
dct = {}
for order in lst:
word_match_ratio = difflib.SequenceMatcher(None, word, order).ratio()
dct[order] = word_match_ratio
print order
print('%s %s' % (order, word_match_ratio))
sorted_matching_words = sorted(dct.items(), key=operator.itemgetter(1))
sorted_matching_words = str(sorted_matching_words.pop()[:1])
x = len(sorted_matching_words) - 3
word = sorted_matching_words[3:x]
print word
def translate(someword):
someword = trans_dct[someword]
print(someword)
return someword
trans_dct = {
"król": 'king'
}
print trans_dct
word = translate(word)
Expected output: king
Instead of that I get:
Traceback (most recent call last):
File "D:/Python/Testing stuff.py", line 64, in <module>
word = translate(word)
File "D:/Python/Playground/Testing stuff.py", line 56, in translate
someword = trans_dct[someword]
KeyError: 'kr\\xf3l'
I don't understand why this happens it looks like difflib is doing something weird because when I do something like this:
uni = 'kr\xf3l'
print uni
def translate(word):
word = dct1[word]
print(word)
return word
dct1 = {
"król": 'king'
}
print dct1
word = translate('kr\xf3l')
print word
Everything works as intended.