<div dir="ltr">2008/10/11 Damian Johnson <span dir="ltr"><<a href="mailto:atagar1@gmail.com">atagar1@gmail.com</a>></span><br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div dir="ltr">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:<br>


</div></blockquote><div><br>Python needs to know the encoding of the bytestring in order to convert it to unicode.  If you don't specify an encoding, ascii is assumed, which doesn't work for any bytestrings that actually contain non-ASCII data.  Since you are reading the string from standard input, try using the encoding associated with stdin:<br>
<br>>>> a = raw_input("text: ")<br>text: おはよう瘢雹<br>>>> b = u"おはよう瘢雹"<br>>>> import sys<br>>>> unicode(a,sys.stdin.encoding) == b<br>True<br><br>Karen<br></div></div><br>
</div>