Special Characters in Console

Discussion in 'Python' started by Gregor, Oct 1, 2003.

  1. Gregor

    Gregor Guest

    I am trying to print some special characters, but they come out as
    different characters in the Python console. If I have a script like this:

    # -*- coding: latin_1 -*-

    print "Mädchen"
    print "M\xE4dchen"

    Both statements produce a capital sigma where they ä should be. Typing
    "\xE4" directly in the console also spits back a sigma.

    What encoding does the console use? Is there a way to get it to use
    latin-1?

    Thanks,

    Greg
     
    Gregor, Oct 1, 2003
    #1
    1. Advertising

  2. Gregor

    Gregor Guest

    I forgot to mention, I'm using Python on Windows...


    Gregor <> wrote in news:ObHeb.1385$6C4.1108
    @pd7tw1no:

    > I am trying to print some special characters, but they come out as
    > different characters in the Python console. If I have a script like

    this:
    >
    > # -*- coding: latin_1 -*-
    >
    > print "Mädchen"
    > print "M\xE4dchen"
    >
    > Both statements produce a capital sigma where they ä should be. Typing
    > "\xE4" directly in the console also spits back a sigma.
    >
    > What encoding does the console use? Is there a way to get it to use
    > latin-1?
    >
    > Thanks,
    >
    > Greg
    >
     
    Gregor, Oct 1, 2003
    #2
    1. Advertising

  3. Gregor

    Neil Hodgson Guest

    Gregor:

    > I am trying to print some special characters, but they come out as
    > different characters in the Python console. If I have a script like this:
    >
    > # -*- coding: latin_1 -*-
    >
    > print "Mädchen"
    > print "M\xE4dchen"
    >
    > Both statements produce a capital sigma where they ä should be. Typing
    > "\xE4" directly in the console also spits back a sigma.
    >
    > What encoding does the console use? Is there a way to get it to use
    > latin-1?


    The windows console is generally using the "Terminal" font with the "DOS:
    Western Europe" character set which displays \xE4 as capital sigma. I don't
    know of an encoding that matches this.

    Neil
     
    Neil Hodgson, Oct 1, 2003
    #3
  4. "Neil Hodgson" <> writes:

    > The windows console is generally using the "Terminal" font with the "DOS:
    > Western Europe" character set which displays \xE4 as capital sigma. I don't
    > know of an encoding that matches this.


    In Python 2.3, using a Unicode string should make it work "out of the
    box". The code page is cp850, but printing on sys.stdout will
    automatically convert to the terminal encoding, so application
    knowledge about the terminal encoding is not needed.

    Regards,
    Martin
     
    Martin v. =?iso-8859-15?q?L=F6wis?=, Oct 1, 2003
    #4
  5. > # -*- coding: latin_1 -*-
    >
    > print "Mädchen"
    > print "M\xE4dchen"
    >
    > Both statements produce a capital sigma where they ä should be. Typing
    > "\xE4" directly in the console also spits back a sigma.


    On the other hand if you capture the output in a file and then load
    it in notepad/vim/pythonwin, you'll see that they look as expected...

    > What encoding does the console use? Is there a way to get it to use
    > latin-1?


    Not that I know of. You might be interested in the win32 port
    of the rxvt terminal which is bundled with the MSYS subproject
    of the MINGW win32 port of gcc:

    http://sourceforge.net/project/showfiles.php?group_id=2435

    Regards,

    Bernard.
     
    Bernard Delmée, Oct 1, 2003
    #5
  6. Gregor

    Duncan Booth Guest

    Gregor <> wrote in
    news:ObHeb.1385$6C4.1108@pd7tw1no:

    > I am trying to print some special characters, but they come out as
    > different characters in the Python console. If I have a script like this:
    >
    > # -*- coding: latin_1 -*-
    >
    > print "Mädchen"
    > print "M\xE4dchen"
    >
    > Both statements produce a capital sigma where they ä should be. Typing
    > "\xE4" directly in the console also spits back a sigma.
    >
    > What encoding does the console use? Is there a way to get it to use
    > latin-1?

    .... plus
    > I forgot to mention, I'm using Python on Windows...


    Assuming a console running in a window (as opposed to running fullscreen)
    then you need to do two things:

    1. The default raster font used for console windows doesn't know about
    latin-1, so change it. Use the system menu, Properties and select "Lucida
    Console" instead of "Raster Fonts".

    2. Use the CHCP command to select the Latin-1 codepage: CHCP 1252

    Now rerun your script and you should see the correct characters.

    Alternatively:

    print "M\xE4dchen".decode('latin1').encode('cp437')

    will get your desired output on codepage 437.

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Oct 2, 2003
    #6
    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. Stefan Mueller
    Replies:
    3
    Views:
    33,051
    Stefan Mueller
    Jul 23, 2006
  2. Replies:
    2
    Views:
    1,099
    Ingo Menger
    May 31, 2007
  3. rvino
    Replies:
    0
    Views:
    4,664
    rvino
    Aug 14, 2007
  4. Replies:
    1
    Views:
    238
    Emile van Sebille
    Mar 6, 2009
  5. majna
    Replies:
    4
    Views:
    678
    Thomas 'PointedEars' Lahn
    Sep 19, 2007
Loading...

Share This Page