如何在 Windows 控制台中显示 utf-8 [英] How to display utf-8 in windows console

查看:28
本文介绍了如何在 Windows 控制台中显示 utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

我在 Windows 7 上使用 Python 2.6

我从这里借用了一些代码:Python、Unicode 和 Windows 控制台

我的目标是能够在 Windows 控制台中显示 uft-8 字符串.

显然在 python 2.6 中,

<块引用>

sys.setdefaultencoding()

不再支持

然而,我在尝试使用它之前编写了 reload(sys) 并且它神奇地没有出错.

此代码不会出错,但会显示有趣的字符而不是日语文本.我认为问题是因为我没有成功更改windows控制台的代码页.

这些是我的尝试,但它们不起作用:

重新加载(系统)sys.setdefaultencoding('utf-8')打印 os.popen('chcp 65001').read()sys.stdout.encoding = 'cp65001'

也许你可以使用 win32console 来更改代码页?我尝试了我链接的网站上的代码,但它也从 win32console 中出错了..也许该代码已过时.

这是我的代码,它不会出错但会打印有趣的字符:

#coding=导入操作系统导入系统导入编解码器重新加载(系统)sys.setdefaultencoding('utf-8')sys.stdout = codecs.getwriter('utf8')(sys.stdout)sys.stderr = codecs.getwriter('utf8')(sys.stderr)#print os.popen('chcp 65001').read()打印(sys.stdout.encoding)sys.stdout.encoding = 'cp65001'打印(sys.stdout.encoding)x = raw_input('按回车继续')a = 'こんにちは世界'#.decode('utf8')打印一个x = raw_input()

解决方案

我知道你说你使用的是 Python 2.6,但如果你能够使用 Python 3.3,你会发现这最终得到了支持.

在启动 Python 之前使用命令 chcp 65001.

参见 http://docs.python.org/dev/whatsnew/3.3.html#codecs

在 Python 3.6 中,甚至不再需要使用 chcp 命令,因为 Python 完全绕过了字节级别的控制台接口,而是使用本机 Unicode 接口.请参阅PEP 528:将 Windows 控制台编码更改为 UTF-8.>

正如@mbom007 在评论中所指出的,确保控制台配置的字体支持您尝试显示的字符也很重要.

I'm using Python 2.6 on Windows 7

I borrowed some code from here: 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()

解决方案

I know you state you're using Python 2.6, but if you're able to use Python 3.3 you'll find that this is finally supported.

Use the command chcp 65001 before starting Python.

See http://docs.python.org/dev/whatsnew/3.3.html#codecs

In Python 3.6 it's no longer even necessary to use the chcp command, since Python bypasses the byte-level console interface entirely and uses a native Unicode interface instead. See PEP 528: Change Windows console encoding to UTF-8.

As noted in the comments by @mbom007, it's also important to make sure the console is configured with a font that supports the characters you're trying to display.

这篇关于如何在 Windows 控制台中显示 utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆