I'm using Python 2.6 on Windows 7
I borrowed some code from here: http://stackoverflow.com/questions/5419/python-unicode-and-the-windows-console
My goal is to be able to display uft-8 strings in the windows console.
Apparantly in python 2.6, the
sys.setdefaultencoding()
is no longer supported
However, I wrote reload(sys) before I tried to use it and it magically didn't error.
This code will NOT error, but it shows funny characters instead of japanese text. I believe the problem is because I have not successfully changed the codepage of the windows console.
These are my attempts, but they don't work:
reload(sys)
sys.setdefaultencoding('utf-8')
print os.popen('chcp 65001').read()
sys.stdout.encoding = 'cp65001'
Perhaps you can use win32console to change the codepage? I tried the code from the website I linked, but it also errored from the win32console.. maybe that code is obsolete.
Here's my code, that doesn't error but prints funny characters:
#coding=<utf8>
import os
import sys
import codecs
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)
#print os.popen('chcp 65001').read()
print(sys.stdout.encoding)
sys.stdout.encoding = 'cp65001'
print(sys.stdout.encoding)
x = raw_input('press enter to continue')
a = 'こんにちは世界'#.decode('utf8')
print a
x = raw_input()