Z
Z
zencd2012-08-25 20:21:00
Python
zencd, 2012-08-25 20:21:00

Python: how to print both Cyrillic and Baltic characters in cmd.exe?

I would like
to a) print Unicode characters from Python to the console and
b) read them immediately with my eyes, without a decoder.
I work through chcp + PYTHONIOENCODING. And so far I can successfully print either Cyrillic (1251) or Baltic (1257), but not mixed - then a UnicodeEncodeError pops up. In general, it’s even clear why all options except the last one don’t work, but it’s not clear why the last one doesn’t work with utf-8. How would you earn it or otherwise get the desired result?
I work in FAR / cmd, so I want everything to start under them. I don’t want to change the sorts either (but if this option is suddenly the only one, please tell us about it).
Python 2.7.3, Windows 7
Run script:

# -*- coding: utf-8 -*-

try:
  print u'Иосиф Кобзон'
except UnicodeEncodeError:
  print 'cannot print cyrillic'

try:
  print u'Eglutės'
except UnicodeEncodeError:
  print 'cannot print baltic'

cmd.exe session log:
$ chcp 1251  1>nul
$ SET PYTHONIOENCODING=1251
$ python enc.py
Иосиф Кобзон
cannot print baltic

$ chcp 1257  1>nul
$ D:\temp\python-output-encoding>SET PYTHONIOENCODING=windows-1257
$ python enc.py
cannot print cyrillic
Eglutės

$ chcp 65001  1>nul
$ SET PYTHONIOENCODING=utf-8
$ python enc.py
��осиф КобзонTraceback (most recent call last):
  File "enc.py", line 4, in <module>
    print u'Иосиф Кобзон'
IOError: [Errno 2] No such file or directory

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anatoly, 2012-08-25
@taliban

Try to run through the power shell and work with utf-8, something may come out.

D
Dmitry Shamov, 2013-11-26
@demmsnt

This is because when the program starts, it finds out in what encoding sys.stdout Yes, you change it, but it already considers that cp866 is there. Therefore, you will have to do unicode('XYZ', 'cp866').encode('baltic_encoding') with your hands and do it through os.system before calling chcp.
Although I suspect that already deduced characters will be spoiled.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question