[Tutor] UnicodeWarning: Comparing String

Prasad, Ramit ramit.prasad at jpmorgan.com
Tue Jan 15 23:26:55 CET 2013


ialectum at gmail.com wrote:
> Hi,
> 
> I am trying to check if a Unicode string matches data from an Easygui "enterbox". I spent one hour looking
> online how to solve this problem and I'm still stuck because I don't understand all of the explanations I find.
> Lots of trial and error didn't help either.
> 
> I use Python 2.5.1.
> 
> ===This is the error message:===
> 
> 
> UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as
> being unequal
> 
> I do understand that everything has to be in Unicode for my code to work, but I do not know how to properly
> change my code.

That is not the full text of the error message. The full text
includes a stack trace and should always be included verbatim. Copy
and paste rather than retyping as accuracy of the message is 
important in helping you.

> 
> ===This is my code:===
> 
> 
> # coding: iso-8859-1
> 
> from easygui import *
> import sys
> 
> good_answer = "pureté"  # notice the accute accent
> 
> answer_given = enterbox("Type your answer!\n\nHint: 'pureté'")

If good_answer is always manually defined (rather than being retrieved
from somewhere, you can just manually set it to Unicode by prefixing
the string with a 'u'.

good_answer = u'pureté' # acute accent and now Unicode!

Otherwise you will have to use one of the below lines.

good_answer.decode(<encoding format>) # converts to Unicode.
# answer_given.encode(<encoding format>) # converts from Unicode to format

> 
> if answer_given == good_answer:
>     msgbox("Correct! The answer is 'pureté'")
> else:
>     msgbox("Bug!")
> 
> Thank you in advance!

Unicode is fairly complex, but maybe this[0] will help if you run into 
problems (or just come back to the list). You can find the list of
encoding formats (called codecs) here[1].

[0] http://docs.python.org/howto/unicode.html
[1] http://docs.python.org/2/library/codecs.html#standard-encodings

Please do not post in HTML or with a colored background.
This list prefers the usage of plain text emails as HTML can
remove indentation and since Python's indentation is 
significant it makes it much more difficult to help you. 
Also, top posting discouraged in favor of either bottom or
in-line posting. Thanks!


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list