Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Encode differences between idle python and python

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


pretoriano_2001 at hotmail

Oct 9, 2006, 10:44 PM

Post #1 of 5 (119 views)
Permalink
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.

--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Oct 9, 2006, 11:23 PM

Post #2 of 5 (105 views)
Permalink
Re: Encode differences between idle python and python [In reply to]

pretoriano_2001 [at] hotmail 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
--
http://mail.python.org/mailman/listinfo/python-list


gagsl-py at yahoo

Oct 10, 2006, 12:24 AM

Post #3 of 5 (105 views)
Permalink
Re: Encode differences between idle python and python [In reply to]

At Tuesday 10/10/2006 02:44, pretoriano_2001 [at] hotmail 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

--
http://mail.python.org/mailman/listinfo/python-list


pretoriano_2001 at hotmail

Oct 10, 2006, 6:55 AM

Post #4 of 5 (100 views)
Permalink
Re: Encode differences between idle python and python [In reply to]

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

Vizcayno

--
http://mail.python.org/mailman/listinfo/python-list


horpner at yahoo

Oct 11, 2006, 5:40 AM

Post #5 of 5 (101 views)
Permalink
Re: Encode differences between idle python and python [In reply to]

On 2006-10-10, pretoriano_2001 [at] hotmail <pretoriano_2001 [at] hotmail> wrote:
>
> 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
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.