469,631 Members | 1,175 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

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'

Apr 6 '06 #1
11 2476
a.*******@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>

Apr 6 '06 #2
(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

Apr 6 '06 #3
Fredrik Lundh wrote:
a.*******@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'

Apr 6 '06 #4
a.*******@vielca.com 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
Apr 6 '06 #5
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

Apr 6 '06 #6

Martin v. Löwis wrote:
a.*******@vielca.com 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

Apr 7 '06 #7
a.*******@vielca.com wrote:
Martin v. Löwis wrote:
a.*******@vielca.com 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


?? 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
import sys
sys.stdin.encoding

'cp1252'

Kent
Apr 7 '06 #8
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 "<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.

Apr 11 '06 #9
a.*******@vielca.com wrote:
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 "<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
Apr 12 '06 #10
-----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:
a.*******@vielca.com wrote:
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 "<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


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

E-Mail: e.*******@nord-com.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: GnuPT 2.7.2
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEPOpuuTzybIiyjvURAqKTAJ9omGK03L9p5dHpzjqN9K z1w6cTYACghO6r
VG30LibkskG9M2boF/lTc0s=
=Xl96
-----END PGP SIGNATURE-----
Apr 12 '06 #11
Egon Frerich wrote:
-----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:
a.*******@vielca.com wrote:
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 "<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


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

E-Mail: e.*******@nord-com.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: GnuPT 2.7.2
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEPOpuuTzybIiyjvURAqKTAJ9omGK03L9p5dHpzjqN9K z1w6cTYACghO6r
VG30LibkskG9M2boF/lTc0s=
=Xl96
-----END PGP SIGNATURE-----

Apr 14 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ivan Voras | last post: by
6 posts views Thread by Spamtrap | last post: by
8 posts views Thread by zahidal | last post: by
6 posts views Thread by John Sidney-Woollett | last post: by
reply views Thread by fructose | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.