473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Encode differences between idle python and python

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
4 2284
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
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 querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Oct 10 '06 #3

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

Vizcayno

Oct 10 '06 #4
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Ashot | last post by:
This is sort of both Python and Vim related (which is why I've posted to both newsgroups). Python related: ---------------------- I have been frustrated for quite some time with a lack of a...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
2
by: Alasdair | last post by:
I've just installed python 2.4.2 from source - it works fine from the command line. But when I attempt to start idle, I am told: ** IDLE can't import Tkinter. Your Python may not be configured...
0
by: h112211 | last post by:
Hi all, I'm using the Windows version of Python 2.4.3 and everything worked okay until I installed PyGreSQL. Well, in fact the installation went fine, but when I try to run my script from IDLE I...
1
by: eastier | last post by:
Hi, I've just read in Python 2.5 description that IDLE 'executes code in a separate process', using a TCP connection on port 127.0.0.1 to communicate. Does it mean that we can now debug...
2
by: dhouse | last post by:
I just installed Python 2.5.1, and I see that IDLE is not included. I have spent the last hour trying to find out how to get IDLE, but have had absolutely no success. It is VERY frustrating. Does...
1
by: sandro dentella | last post by:
Hi, I'm having a little problem while using python mode. I'm used to hit C-c C-c and have a Python Output buffer opened with the output. Now, on Ubuntu 7.10 and 8.04, C-c C-c sends to the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.