I have written a program in Tkinter (Python 2.7), a scrabblehelper in Norwegian which contains some special characters (æøå
), which means my wordlist (ordliste) contains words with special characters.
When I run my function finnord(c*), it returns 'cd'. I am using an entry.get()
to get the word to put in my function.
My problem is with the encoding of entry.get(). I have local coding UTF-8
, but I get an UniCodeError
when I am writing any special characters in my entrybox and matching them to my wordliste.
Here is my output.
Warning (from warnings module):
File "C:\pythonprog\scrabble\feud.py", line 46
if s not in liste and s in ordliste:
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode -
interpreting them as being unequal
When i write in my shell:
> ordinn.get()
u'k\xf8**e'
> ordinn.get().encode('utf-8')
'k\xc3\xb8**e'
> print ordinn.get()
kø**e
> print ordinn.get().encode('utf-8')
kø**e
Anyone knows why I can't match ordinn.get() (entry) to my wordlist ?
I can reproduce the error this way:
So perhaps
s
is astr object
, andliste
orordliste
containsunicode
, or (as eryksun points out in the comments) vice versa. The solution is to decode thestr object
s (most likely with theutf-8
codec) to make themunicode
.If that does not help, please print out and post the output of
I believe the problem can be avoided by converting all strings to
unicode
.When you generate
ordliste
fromnorsk.txt
, usecodecs.open('norsk.txt','r','utf-8')
:Convert all user input to unicode as soon as possible:
So perhaps try this: