Unicode, command-line and idle

a.serrano at vielca.com a.serrano at vielca.com
Thu Apr 6 10:25:36 EDT 2006


Fredrik Lundh wrote:
> 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>

This works if I use the console but gives the following error if I use
IDLE:

Traceback (most recent call last):
  File "C:\test.py", line 4, in ?
    text2 = unicode(raw_input(), sys.stdin.encoding)
AttributeError: PyShell instance has no attribute 'encoding'




More information about the Python-list mailing list