简体   繁体   中英

How to print japanese utf-8 on console in windows?

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM