Special Characters in Console

G

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?

Thanks,

Greg
 
N

Neil Hodgson

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
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

Neil Hodgson said:
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
 
B

Bernard Delmée

# -*- 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.
 
D

Duncan Booth

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top