473,320 Members | 1,867 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,320 software developers and data experts.

How to emit UTF-8 from console mode?

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[:utf8]; print chr() for 0x410 .. 0x430;"

This little one liner prints the Russian alphabet in Cryllic. With some
slight modification it will also print a lot of other alphabets too --
including Hebrew, chinese and japanese.

It does not work with cmd.exe because apparently cmd.exe cannot deal with
UTF-8.

Can someone help me translate it into python? I would not expect it to work
from cmd.exe with python, but I am hopeful it will work with urxvt-X!

Thanks,
Siegfried
Oct 1 '08 #1
8 7006
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

Oct 1 '08 #2

""Martin v. Löwis"" <ma****@v.loewis.dewrote in message
news:48**********************@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

Oct 1 '08 #3
>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:

?????????????????????????????????
Wowa! I was not aware of that chcp command! Thanks! How could I do that
"chcp 1251" programatically?

The code was a little confusing because those two apostrophes look like a
double quote!

But what are we doing here? Can you convince me that we are emitting UTF-8?
I need UTF-8 because I need to experiment with some OS function calls that
give me UTF-16 and I need to emit UTF-16 or UTF-8.

I think part of the problem is that Lucida Console is not as capable as
"Arial Unicode MS" or the fonts used by urxvt-X.

Thanks,
Siegfried
Oct 1 '08 #4
On Wed, 01 Oct 2008 08:17:15 -0700, Siegfried Heintze wrote:

(snip)
The code was a little confusing because those two apostrophes look like
a double quote!
Tips: use mono-spaced font. There is no ambiguity.

(snip)
I think part of the problem is that Lucida Console is not as capable as
"Arial Unicode MS" or the fonts used by urxvt-X.

Thanks,
Siegfried
Why don't you write it to a file? Then open that file from Notepad

Oct 1 '08 #5
But what are we doing here? Can you convince me that we are emitting UTF-8?

Most definitely not. We are emitting cp1251.
I need UTF-8 because I need to experiment with some OS function calls that
give me UTF-16 and I need to emit UTF-16 or UTF-8.
Try setting the code page to 65001, and emit the UTF-8 explicitly.

Regards,
Martin
Oct 1 '08 #6
I need UTF-8 because I need to experiment with some OS function calls that
give me UTF-16 and I need to emit UTF-16 or UTF-8.
<ma****@v.loewis.dewrote:
>Try setting the code page to 65001, and emit the UTF-8 explicitly.
Hmm... apparently that's not allowed on Windows XP:

C:\chcp 65001
Active code page: 65001

C:\python -c "for i in range(0x410, 0x430): print unichr(i).encode('utf-8')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
IOError: [Errno 13] Permission denied

This works though:

C:\python -c "for i in range(0x410, 0x430): print unichr(i).encode('utf-8')" x

C:\type x
[a bunch of Cyrillic letters]

Hmm... "more x" doesn't work, while "copy x con" works but gives an error.
Looks like Windows XP support UTF-8 console output is a bit half-assed.

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rr****@csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
Oct 1 '08 #7

"Ross Ridge" <rr****@csclub.uwaterloo.cawrote in message
news:gc**********@rumours.uwaterloo.ca...
>I need UTF-8 because I need to experiment with some OS function calls
that
give me UTF-16 and I need to emit UTF-16 or UTF-8.

<ma****@v.loewis.dewrote:
>>Try setting the code page to 65001, and emit the UTF-8 explicitly.

Hmm... apparently that's not allowed on Windows XP:

C:\chcp 65001
Active code page: 65001

C:\python -c "for i in range(0x410, 0x430): print
unichr(i).encode('utf-8')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
IOError: [Errno 13] Permission denied

This works though:

C:\python -c "for i in range(0x410, 0x430): print
unichr(i).encode('utf-8')" x

C:\type x
[a bunch of Cyrillic letters]

Hmm... "more x" doesn't work, while "copy x con" works but gives an error.
Looks like Windows XP support UTF-8 console output is a bit half-assed.
It is odd, though, that when the code page (and font) are correct,
redirecting to a file and typing it work, but printing the result to the
console does not

Oct 2 '08 #8

"Siegfried Heintze" <si*******@heintze.comwrote in message
news:vL******************************@comcast.com. ..
>
>>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:

?????????????????????????????????
Wowa! I was not aware of that chcp command! Thanks! How could I do that
"chcp 1251" programatically?

The code was a little confusing because those two apostrophes look like a
double quote!

But what are we doing here? Can you convince me that we are emitting
UTF-8? I need UTF-8 because I need to experiment with some OS function
calls that give me UTF-16 and I need to emit UTF-16 or UTF-8.

I think part of the problem is that Lucida Console is not as capable as
"Arial Unicode MS" or the fonts used by urxvt-X.
In this case, it is not emitting UTF-8. It is emitting the windows-1251
encoding. As another poster mentioned, the Windows console gets an error
when attempting to write UTF8 when the code page is 65001 (UTF8). But you
can write output to a file explicitly in UTF-8 or UTF-16 and view the file
with Notepad. I've used this method for processing Chinese.
>>import os,codecs
data = u''.join(unichr(i) for i in range(0x410,0x431))
codecs.open('out.txt','wt','utf-8').write(data)
os.startfile('out.txt')
P.S.

One way to set the code page programmatically is to use ctypes, but this
will only work in a Windows console:
>>import ctypes
k=ctypes.WinDLL('kernel32')
x.SetConsoleOutputCP(1251)
1
>>print u''.join(unichr(i) for i in
range(0x410,0x430)).encode('windows-1251')
АБВГДЕЖЗИЙКЛМНОП*СТУФХЦЧШ ЩЪЫЬ*ЮЯ

--Mark

Oct 2 '08 #9

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

Similar topics

0
by: Nigel Sampson | last post by:
ey all, I'm in the process of creating a method of tracking whenever an objects properties are changed. Since its not possible to alter existing types I decided to used a proxy generated using...
2
by: DEK | last post by:
I'm creating a dynamic assembly which has one type with one constructor, the type inherits a base class Season, which is in the current assembly. I am trying to emit a constructor which simply...
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
3
by: John Arthur | last post by:
Hi, I am reading about Reflection.Emit and I can't really imagine a real situation where I can use this. Can someone give me an example that can't be done without Reflection or an example of...
8
by: Eyeawanda Pondicherry | last post by:
I have put some code together that creates an enum dynamically from some database values. The enum can be read perfectly by an application that references the dynamically generated dll. If I...
4
by: Gianluca | last post by:
This is the simplified code I'm trying to generate: ilg.DeclareLocal(typeof(int)); // define local integer Label exit = ilg.DefineLabel(); // define "exit" label ...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
1
by: John | last post by:
I am trying to figure out how to translate the following CIL call into an Emit statement. In CIL it is: IL_004c: callvirt instance class System.Data.DataRow...
2
by: joasi | last post by:
Hi, I am working in C# with an Object / Relational Database mapping framework, where I want to generate method calls based on string representation of the call signature. I have used reflection and...
0
by: =?Utf-8?B?QXJ0dXJvIE1hcnRpbmV6?= | last post by:
I've been looking everywhere on how to call AddRange Method of a List<myClassfield in Reflection.Emit My code goes like this MethodInfo method =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.