Encode differences between idle python and python

Discussion in 'Python' started by pretoriano_2001@hotmail.com, Oct 10, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Peter Otten Guest

    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
    Peter Otten, Oct 10, 2006
    #2
    1. Advertising

  3. At Tuesday 10/10/2006 02:44, 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
    Gabriel Genellina, Oct 10, 2006
    #3
  4. Guest

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

    Vizcayno
    , Oct 10, 2006
    #4
  5. Neil Cerutti Guest

    On 2006-10-10, <> 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
    Neil Cerutti, Oct 11, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    0
    Views:
    394
    Chris
    Apr 14, 2004
  2. Eric S. Johansson

    differences between ubuntu and fedora python

    Eric S. Johansson, Dec 7, 2006, in forum: Python
    Replies:
    0
    Views:
    285
    Eric S. Johansson
    Dec 7, 2006
  3. Home_Job_opportunity
    Replies:
    0
    Views:
    497
    Home_Job_opportunity
    Jan 8, 2009
  4. Home_Job_opportunity
    Replies:
    0
    Views:
    584
    Home_Job_opportunity
    Jan 14, 2009
  5. ArrC
    Replies:
    4
    Views:
    975
    Terry Reedy
    Jul 13, 2011
Loading...

Share This Page