Unicode, command-line and idle

Fredrik Lundh fredrik at pythonware.com
Thu Apr 6 07:18:14 EDT 2006


a.serrano at vielca.com wrote:

> Hello, the following program prompts the user for a word and tests to
> see if it is the same as another one. If the user types "españa" (note
> that the word contains an 'ñ'), the program should output "same". This
> works if I run the code in IDLE but does not if I run it in the windows
> console. Can someone explain me why this happens and how to get around
> it?
>
> # -*- coding: cp1252 -*-
> text1 = 'españa'
> text2 = raw_input()
> if text1 == text2:
>     print 'same'
> else:
>     print 'not same'

I don't think raw_input decodes non-ascii input at all. and your console
probably uses cp850 rather than cp1252.  doing the comparision over in
unicode space should work better:

    import sys
    text1 = u'españa'
    text2 = unicode(raw_input(), sys.stdin.encoding)
    if text1 == text2:
        print 'same'
    else:
        print 'not same'

</F>






More information about the Python-list mailing list