0
#coding=<utf8>
import os
os.popen('chcp 65001')

a = 'こんにちは世界'
print a.decode('utf8')

x = raw_input()

PYTHON 2.6 on Windows 7

It will run in IDLE with no errors.

However when run from the console, it errors and flashes very quickly and I can't read the error message. How can it be done in windows console?

By the way, doing this with other languages like spanish or portuguese will work fine. It's languages like japanese, russian, greek, hebrew that have this error behavior in the windows console.

*EDIT as requested I changed to this code:

#coding=<utf8>
import os, sys
os.popen('chcp 65001')

print(sys.stdout.encoding)
x = raw_input('press enter to continue')

a = 'こんにちは世界'
print a.decode('utf8')

x = raw_input()

It will print: cp437

and then of course, continue on to flash and fail on the decoding bit...

It looks like the popen('chcp 65001') doesn't work in changing the codepage. I still don't think this is the root of the problem, however it would be helpful to know an efficient way of changing this codepage.

russo
  • 271
  • 2
  • 3
  • 9
  • In the script, add `import sys` and `print(sys.stdout.encoding)`. Please post the result. – unutbu Aug 26 '10 at 18:19
  • The fonts available in Windows consoles include only a few scripts. – Adrian McCarthy Aug 26 '10 at 19:49
  • This is a DUP of your own question http://stackoverflow.com/questions/3578685/how-to-display-utf-8-in-windows-console Edit the original question and its title; then request this question to be closed. BE RESPONSIBLE – OTZ Aug 27 '10 at 04:16
  • I don't like to add multiple questions to an open question, that question deals with something similar and related, but not the same question. That's my philosophy on questions, and that's why I created a separate question. We can't just throw all related questions into one thread, how will we find specific answers then.. or clear confusion for the answereres, if we're asking many questions in one question thread... – russo Aug 27 '10 at 06:11

1 Answers1

0

Update

Never mind. The OP is using Windows.

Interestingly changing the encoding declaration to #encoding=<utf8> did not work in Ubuntu.

Original Answer

This worked for me (Ubuntu Jaunty, Python 2.6.2). The only change I made was to the first line declaring the encoding.

# encoding: utf-8  
import os
os.popen('chcp 65001')

a = 'こんにちは世界'
print a.decode('utf8')

x = raw_input()
Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141
  • Yes, it looks like you are using linux. It errors for me on the windows console. There a a couple different ways to specify the encoding of the script, both are correct. – russo Aug 26 '10 at 18:17
  • Ya, mistype meant to write 'coding' not 'encoding' Here's the page where it says the format: http://www.python.org/dev/peps/pep-0263/ – russo Aug 26 '10 at 18:31