473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to emit Cyrillic and Chinese via unicode from console mode?

Can someone point me to an example of a little program that emits non-ascii
Unicode characters (Russian or Chinese perhaps)? The unicode
Russian/Cyrillic alphabet starts at 0x410. Is this possible to do in a
console mode program? If not, I guess I would want to use pywin32 to create
a window and a message pump and display it there. I anticipate using pywin32
for some other function calls.

Thanks!
Siegfried
Sep 14 '08 #1
5 2054
On Sep 14, 2:03Â*am, "Siegfried Heintze" <siegfr...@heintze.comwrote:
Can someone point me to an example of a little program that emits non-ascii
Unicode characters (Russian or Chinese perhaps)?
The following doesn't quite work, but I'll post it anyway since it
actually ends up printing the characters. Perhaps someone can point
out what causes the exception at the end?

The important thing is to set the console codepage to 65001, which is
UTF-8. This lets you output utf8-encoded text and see the Unicode
chars displayed.

import sys
import encodings.utf_8
import win32console

sys.stdout = encodings.utf_8.StreamWriter(sys.stdout)

win32console.SetConsoleCP(65001)
win32console.SetConsoleOutputCP(65001)

s = "English: ok\n"
s += u'Russian: \u0420\u0443\u0441\u0441\u043a\u0438\u0439\n'
s += u'Greek: \u03bc\u03b5\u03b3\u03b1\u03bb\u03cd
\u03c4\u03b5\u03c1\u03b7\n'

print s

If redirected to file, all is well, this prints everything properly in
UTF-8. If ran on the console, this also prints everything correctly,
but then throws a mysterious exception:

English: ok
Russian: Ð*уÑÑкий
Greek: μεγαλÏτεÏη
Traceback (most recent call last):
File "I:\Temp\utf8console.py", line 18, in <module>
print s
File "C:\Progs\Python25\lib\codecs.py", line 304, in write
self.stream.write(data)
IOError: [Errno 0] Error

Any ideas?

Roman

P.S. This really ought to Just Work in this day and age, and do so
without all those 65001/utf8 incantations. Pity that it doesn't. Sigh.
Sep 14 '08 #2
rs387 wrote:
>sys.stdout = encodings.utf_8.StreamWriter(sys.stdout)

win32console.SetConsoleCP(65001)
win32console.SetConsoleOutputCP(65001)
[...]
>If redirected to file, all is well, this prints everything properly in
UTF-8. If ran on the console, this also prints everything correctly,
but then throws a mysterious exception:
Interesting. On my system (Windows XP) the console codepage does not
change, and hence the characters don't print properly (I get some of the
CP437 line drawing characters, for example). I have never been able to
convince windows to assume/support UTF-8 encoding in the console,
programatically or otherwise. :(

Gertjan.

--
Gertjan Klein <gk****@xs4all.nl>
Sep 14 '08 #3
On Sep 14, 11:51*am, Gertjan Klein <gkl...@xs4all.nlwrote:
Interesting. On my system (Windows XP) the console codepage does not
change, and hence the characters don't print properly (I get some of the
CP437 line drawing characters, for example). I have never been able to
convince windows to assume/support UTF-8 encoding in the console,
programatically or otherwise. :(
I found that a useful test is to create a directory whose name
contains chars from various languages, then run "cmd" and see if "dir"
displays them correctly. This worked fine for me in WinXP, even though
my system locale is Russian. Would be interesting to know if you can
get the console to display international chars this way.
Sep 14 '08 #4
On Sun, 14 Sep 2008 01:02:39 -0700 (PDT)
rs387 <rs******@gmail.comwrote:
On Sep 14, 2:03Â*am, "Siegfried Heintze" <siegfr...@heintze.comwrote:
Can someone point me to an example of a little program that emits
non-ascii Unicode characters (Russian or Chinese perhaps)?
The following doesn't quite work, but I'll post it anyway since it
actually ends up printing the characters.
That's more like it! Just answer with whatever one has. Here's another
gem:

from Tkinter import *
from collections import deque

def byn(x,n =5 ):
L = deque(x)
R = []
while L:
R.append(L.popleft())
if len(R) == n:
yield ''.join(R)
R = []
if R:
yield ''.join(R)

root = Tk()
start = int('16A6',16)
end = int('16F0',16)
g = (unichr(i) for i in xrange(start, end+1))
L = byn(g,16)
s = '\n'.join(L)
w = Label(root, text=s,font = ("freemono","80"))
w.pack()

root.mainloop()

P.

Sep 14 '08 #5
Can someone point me to an example of a little program that emits non-ascii
Unicode characters (Russian or Chinese perhaps)? The unicode
Russian/Cyrillic alphabet starts at 0x410. Is this possible to do in a
console mode program? If not, I guess I would want to use pywin32 to create
a window and a message pump and display it there. I anticipate using pywin32
for some other function calls.
It also depends on your console. On Linux, with an UTF-8
capable-and-enabled console,

pyprint u"\u0413"
Г

work just fine.

Regards,
Martin
Sep 14 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: pabv | last post by:
Hello all, I am having a few issues with encoding to chinese characters and perhaps someone might be able to assist. At the moment I am only able to see chinese characters when displayed as...
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
8
by: Kirill Simonov | last post by:
Hi, Could anyone suggest me a simple IDE suitable for teaching Python as a first programming language to high school students? It is necessary that it has a good support for input/output in...
8
by: Siegfried Heintze | last post by:
The following perl program works when I run it from urxvt-X console on cygwin-x windows LC_CTYPE=en_US.UTF-8 urxvt-X.exe& perl -wle "binmode STDOUT, q; print chr() for 0x410 .. 0x430;" This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.