By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,857 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

Encode differences between idle python and python

P: n/a
Hello:
Under win32 XP y select python command line and execute next code with
results indicated:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>>u=u''
u
u'\xe1\xe9\xed\xf3\xfa'
>>print u
>>a=u.encode('latin-1')
a
'\xe1\xe9\xed\xf3\xfa'
>>print a
ݾ
>>type(a)
<type 'str'>
>>type(u)
<type 'unicode'>
>>>
using python IDLE I repeat the code, but get next differen result:
IDLE 1.2
>>u=u''
u
u'\xe1\xe9\xed\xf3\xfa'
>>print u
>>a=u.encode('latin-1')
a
'\xe1\xe9\xed\xf3\xfa'
>>print a
>>type(a)
<type 'str'>
>>type(u)
<type 'unicode'>
>>>
What do you think is happending and how can I solve this ? The IDLE
looks fine but command line has problems.

Oct 10 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
pr*************@hotmail.com wrote:
>u=u''
u
u'\xe1\xe9\xed\xf3\xfa'
>print u
>a=u.encode('latin-1')
a
'\xe1\xe9\xed\xf3\xfa'
>print a
ݾ
That means that Python is better at guessing the correct encoding than you
are. Here's how you can make it share its secrets:
>>import sys
sys.stdout.encoding
'UTF-8' # something else on your machine (cp850, maybe)

Then you can use that encoding to print:
>>your_encoding = sys.stdout.encoding
print u"".encode(your_encoding)


On the other hand: why not always print the unicode string directly?

Peter
Oct 10 '06 #2

P: n/a
At Tuesday 10/10/2006 02:44, pr*************@hotmail.com wrote:
>Hello:
Under win32 XP y select python command line and execute next code with
results indicated:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>u=u''
u
u'\xe1\xe9\xed\xf3\xfa'
>print u
>a=u.encode('latin-1')
a
'\xe1\xe9\xed\xf3\xfa'
>print a
ݾ
>type(a)
<type 'str'>
>type(u)
<type 'unicode'>
>>
Because the console code page != windows code page.
Exit Python. At the console prompt, type:
>chcp
If it says 850 - your console is using codepage 850.
Enter Python again, and replace 'latin-1' with
'cp850'. You should get the right representation.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Pregunt. Respond. Descubr.
Todo lo que queras saber, y lo que ni imaginabas,
est en Yahoo! Respuestas (Beta).
Probalo ya!
http://www.yahoo.com.ar/respuestas

Oct 10 '06 #3

P: n/a

Gabriel, Peter:
Many thanks for your clear answers!!
Best regards.

Vizcayno

Oct 10 '06 #4

P: n/a
On 2006-10-10, pr*************@hotmail.com <pr*************@hotmail.comwrote:
>
Gabriel, Peter:
Many thanks for your clear answers!! Best regards.
Something I've been working on is currently using the following
trick:

# Create some string of non-ASCII text in ISO 8859-1.
some_string = ''.join(chr(a) for a in range(0xc0, 0xdf)).decode('ISO 8859-1')
# Print it to stdout, converting to the terminal's encoding, replacing
# unprintable characters with '?'.
print some_string.encode(sys.stdout.encoding, 'replace')

--
Neil Cerutti
That's the biggest laughingstock I've ever heard of in my life.
--Trot Nixon
Oct 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.