Encode differences between idle python and python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pretoriano_2001@hotmail.com

    #1

    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('l atin-1')
    >>a
    '\xe1\xe9\xed\x f3\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('l atin-1')
    >>a
    '\xe1\xe9\xed\x f3\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.

  • Peter Otten

    #2
    Re: Encode differences between idle python and python

    pretoriano_2001 @hotmail.com wrote:
    >u=u'áéíóú'
    >u
    u'\xe1\xe9\xed\ xf3\xfa'
    >print u
    áéíóú
    >a=u.encode('la tin-1')
    >a
    '\xe1\xe9\xed\x f3\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.en coding
    'UTF-8' # something else on your machine (cp850, maybe)

    Then you can use that encoding to print:
    >>your_encodi ng = sys.stdout.enco ding
    >>print u"áéíóú".encode (your_encoding)
    áéíóú

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

    Peter

    Comment

    • Gabriel Genellina

      #3
      Re: Encode differences between idle python and python

      At Tuesday 10/10/2006 02:44, pretoriano_2001 @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('la tin-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!


      Comment

      • pretoriano_2001@hotmail.com

        #4
        Re: Encode differences between idle python and python


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

        Vizcayno

        Comment

        • Neil Cerutti

          #5
          Re: Encode differences between idle python and python

          On 2006-10-10, pretoriano_2001 @hotmail.com <pretoriano_200 1@hotmail.comwr ote:
          >
          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.enc ode(sys.stdout. encoding, 'replace')

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

          Comment

          Working...