How to emit UTF-8 from console mode?

Mark Tolonen M8R-yfto6h at mailinator.com
Wed Oct 1 02:55:02 EDT 2008


""Martin v. Löwis"" <martin at v.loewis.de> wrote in message 
news:48e31066$0$2548$9b622d9e at news.freenet.de...
>> LC_CTYPE=en_US.UTF-8 urxvt-X.exe&
>> perl -wle "binmode STDOUT, q[:utf8]; print chr() for 0x410 .. 0x430;"
>
>> Can someone help me translate it into python?
>
> LC_CTYPE=en_US.UTF-8 urxvt-X.exe&
> python -c 'for i in range(0x410, 0x431):print unichr(i),'
>
>> I would not expect it to work
>> from cmd.exe with python
>
> It should work in cmd.exe, as long as the terminal's encoding supports
> these characters in the first place. Use chcp.exe to find out what the
> terminal's encoding is. The Python program is not completely equivalent,
> as it leaves the output encoding to Python, rather than assuming a fixed
> UTF-8 output encoding.
>
> Regards,
> Martin

Make sure you are using the Lucida Console font for the cmd.exe window and 
type the commands:

chcp 1251
python -c "print ''.join(unichr(i) for i in range(0x410,0x431))"

Output:

АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯа

UTF-8 encoding (chcp 65001)  doesn't work (Python doesn't recognize it: 
"LookupError: unknown encoding: cp65001") and I couldn't get any Chinese 
code pages to work either.  There is some trick I don't know, because 
Chinese versions of Windows can display Chinese.  I have the East Asian 
languages installed and Chinese IME enabled, but it doesn't help for console 
apps.

--Mark




More information about the Python-list mailing list