Unicode, command-line and idle
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' -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode, command-line and idle
Fredrik Lundh wrote: > [EMAIL PROTECTED] 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' > > 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' -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode, command-line and idle
Martin v. Löwis wrote: > [EMAIL PROTECTED] wrote: > > 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' > > What operating system and Python version are you using? This ought to > work. > > Regards, > Martin Python 2.4.3 IDLE 1.1.3 Windows XP SP2 -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode, command-line and idle
Hello again, I've investigated a little bit and this is what I found: If I run IDLE and type >>> import sys >>> sys.stdin.encoding I get 'cp1252' But if I have a whatever.py file (it can even be a blank file), I edit it with IDLE, I press F5 (Run Module) and then type: >>> import sys >>> sys.stdin.encoding I get Traceback (most recent call last): File "", line 1, in ? sys.stdin.encoding AttributeError: PyShell instance has no attribute 'encoding' So when I have the following code in a file: # -*- coding: cp1252 -*- import sys text1 = u'españa' text2 = unicode(raw_input(), sys.stdin.encoding) if text1 == text2: print 'same' else: print 'not same' and I press F5 (Run Module) I get: 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' This same code works if I just double-click it (run it in the windows console) instead of using IDLE. I'm using Python 2.4.3 and IDLE 1.1.3. Armando. -- http://mail.python.org/mailman/listinfo/python-list
