tags:

views:

50

answers:

1
#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.

A: 

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
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
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
@russo: I didn't know that. Thanks.
Manoj Govindan