在 Windows 上设置 Python 终端编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6179617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:57:55  来源:igfitidea点击:

Set Python terminal encoding on Windows

pythonwindowscharacter-encoding

提问by Arnthor

I happened to fail to set character encoding in Python terminal on Windows. According to official guide, it's a piece of cake:

我碰巧在 Windows 上的 Python 终端中设置字符编码失败。根据官方指南,这是小菜一碟:

# -*- coding: utf-8 -*-

Ok, now testing:

好的,现在测试:

print 'Русский'

Produces piece of mojibake. What am doing wrong?

生产一块 mojibake。做错了什么?

P.S.IDE is Visual Studio 2010, if it matters

PSIDE 是 Visual Studio 2010,如果重要的话

回答by JBernardo

You should use unicode:

你应该使用 unicode:

print u'Русский'

or switch to python3 (unicode by default).

或切换到 python3(默认为 unicode)。

回答by Eser Aygün

Update: See J.F. Sebastian's answerfor a better explanation and a better solution.

更新:请参阅JF Sebastian 的回答以获得更好的解释和更好的解决方案。

# -*- coding: utf-8 -*-sets the source file's encoding, not the output encoding.

# -*- coding: utf-8 -*-设置源文件的编码,而不是输出编码。

You have to encode the string just before printing it with the exact same encoding that your terminal is using. In your case, I'm guessing that your code page is Cyrillic (cp866). Therefore,

您必须在打印之前使用与终端使用的完全相同的编码对字符串进行编码。就您而言,我猜您的代码页是西里尔文 (cp866)。所以,

print 'Русский'.encode("cp866")

回答by jfs

It produces mojibake because ''is a bytestring literal in Python 2 (unless from __future__ import unicode_literalsis used). You are printing utf-8 bytes (the source code encoding) to Windows console that uses some othercharacter encoding (the encoding is different if you see mojibake):

它产生 mojibake 因为它''是 Python 2 中的字节串文字(除非from __future__ import unicode_literals使用)。您正在将 utf-8 字节(源代码编码)打印到使用其他字符编码的Windows 控制台(如果您看到 mojibake,则编码会有所不同):

>>> print(u'Русский'.encode('utf-8').decode('cp866'))
╨а╤Г╤Б╤Б╨║╨╕╨╣

The solution is to print Unicode instead as @JBernardo suggested:

解决方案是按照@JBernardo 的建议打印 Unicode :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
print(u'Русский')

It works if the console encoding supports Cyrillic letters e.g., if it is cp866.

如果控制台编码支持西里尔字母(例如,如果它是cp866.

If you want to redirect the output to a file; you could use PYTHONIOENCODINGenvironment variable to set the character encoding used by Python for I/O:

如果要将输出重定向到文件;您可以使用PYTHONIOENCODING环境变量来设置 Python 用于 I/O 的字符编码:

Z:\> set PYTHONIOENCODING=utf-8
Z:\> python your_script.py > output.utf-8.txt

If you want to print Unicode characters that can't be represented using the console encoding(OEM code page) then you could install win-unicode-consolePython package:

如果要打印无法使用控制台编码OEM 代码页表示的 Unicode 字符,则可以安装win-unicode-consolePython 包

Z:\> py -m pip install win_unicode_console
Z:\> py -m run your_script.py

回答by lxx

In case anyone else gets this page when searching easiest is to set the windows terminal code page

如果其他人在搜索时获得此页面,最简单的方法是设置 windows 终端代码页

CHCP 65001

or for power shell start it with

或对于电源外壳启动它

powershell.exe -NoExit /c "chcp.com 65001"

from Is there a Windows command shell that will display Unicode characters?

来自是否有可以显示 Unicode 字符的 Windows 命令外壳?