Unicode, command-line and idle

A

a.serrano

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'
 
F

Fredrik Lundh

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>
 
M

Méta-MCI

(just for confirm)


Hi!


if the console is in cp1252, raw_input work OK with "ñ"

This (your) script :


# -*- coding: cp1252 -*-

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


work OK with "chcp 850" AND "chcp 1252"





@-salutations

Michel Claveau
 
A

a.serrano

Fredrik said:
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'
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

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
 
J

John Machin

The docs say that the encoding attribute is "New in version 2.3".
Python 2.2's IDLE produces exactly the exception reported by the OP.
Cheers,
John
 
K

Kent Johnson

Python 2.4.3
IDLE 1.1.3
Windows XP SP2

?? Works for me on Win2K

Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license()" for more information.

IDLE 1.1.3'cp1252'

Kent
 
A

a.serrano

Hello again, I've investigated a little bit and this is what I found:

If I run IDLE and type

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:

I get

Traceback (most recent call last):
File "<pyshell#1>", 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.
 
K

Kent Johnson

Hello again, I've investigated a little bit and this is what I found:

If I run IDLE and type


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:


I get

Traceback (most recent call last):
File "<pyshell#1>", 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.

FWIW all of the above give me 'cp1252', not AttributeError, and the type
of sys.stdin on my system is idlelib.rpc.RPCProxy, not PyShell.

Python 2.4.3 and IDLE 1.1.3 on Win2k

Kent
 
E

Egon Frerich

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

What do you have in the IDLE options - General - Default source encoding?

Egon

Kent Johnson schrieb am 12.04.2006 12:40:
FWIW all of the above give me 'cp1252', not AttributeError, and the type
of sys.stdin on my system is idlelib.rpc.RPCProxy, not PyShell.

Python 2.4.3 and IDLE 1.1.3 on Win2k

Kent

- --
Egon Frerich, Freudenbergstr. 16, 28213 Bremen

E-Mail: (e-mail address removed)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: GnuPT 2.7.2
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEPOpuuTzybIiyjvURAqKTAJ9omGK03L9p5dHpzjqN9Kz1w6cTYACghO6r
VG30LibkskG9M2boF/lTc0s=
=Xl96
-----END PGP SIGNATURE-----
 
K

Kent Johnson

Egon said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

What do you have in the IDLE options - General - Default source encoding?
UTF-8


Egon

Kent Johnson schrieb am 12.04.2006 12:40:


- --
Egon Frerich, Freudenbergstr. 16, 28213 Bremen

E-Mail: (e-mail address removed)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: GnuPT 2.7.2
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEPOpuuTzybIiyjvURAqKTAJ9omGK03L9p5dHpzjqN9Kz1w6cTYACghO6r
VG30LibkskG9M2boF/lTc0s=
=Xl96
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top