Unicode equality from raw_input

Damian Johnson atagar1 at gmail.com
Sat Oct 11 20:25:46 EDT 2008


Hi, when getting text via the raw_input method it's always a string (even if
it contains non-ASCII characters). The problem lies in that whenever I try
to check equality against a Unicode string it fails. I've tried using the
unicode method to 'cast' the string to the Unicode type but this throws an
exception:

>>> a = raw_input("text: ")
text: おはよう
>>> b = u"おはよう"
>>> a == b
__main__:1: UnicodeWarning: Unicode equal comparison failed to convert both
arguments to Unicode - interpreting them as being unequal
False
>>> type(a)
<type 'str'>
>>> type(b)
<type 'unicode'>
>>> unicode(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0:
ordinal not in range(128)
>>> str(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3:
ordinal not in range(128)


After a couple hours of hair pulling I think it's about time to admit
defeat. Any help would be appreciated! -Damian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081011/f07e2886/attachment.html>


More information about the Python-list mailing list