Sublime Text 控制台不显示带有重音符号的行

Sublime Text console not showing lines with accents

在 Sublime Text 2 和 3 中,控制台输出不显示带有重音符号的行:

我在 Windows 中使用 vanilla Sublime 中的 Tools > Build 和自动构建系统来执行它。

有什么办法解决这个问题吗?

将文档中标准系统输出的编码设置为UTF-8:

import sys
import codecs

sys.stdout = codecs.getwriter( "utf-8" )( sys.stdout.detach() )

print( "1" )
print( "áéíóúý âêîôû äëïöü àèìòù ãñõ" )
print( "2" )

要自动将 UTF-8 编码输出应用到所有文档,请在您的 Python.sublime-build 文件中将上述方法作为内联 command 实现。

设置编码后,您的文档将通过内联 command.

中的 exec 加载
{
    "cmd": [ "python", "-u", "-c", "import sys; import codecs; sys.stdout = codecs.getwriter( 'utf-8' )( sys.stdout.detach() ); exec( compile( open( r'$file', 'rb' ).read(), r'$file', 'exec'), globals(), locals() )" ],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "python -m py_compile \"${file}\"",
        }
    ]
}

提示: 使用 PackageResourceViewer to create a of Python.sublime-build


已使用 Sublime Text 3稳定频道,内部版本 3103)和 Python 3.4.3

进行测试

来源:

How to set sys.stdout encoding in Python 3?

Alternative to execfile in Python 3?

更简洁的解决方案是将编码指定为构建设置的一部分

{   
    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "env": {
        "PYTHONIOENCODING": "utf_8"
    },
}

这在大多数情况下都有效。但在某些情况下,您可能需要删除 -u,这基本上是为了停止无缓冲输出,因为它可能会导致问题

有关类似问题的讨论,请参阅下面的讨论帖

Sublime Text 3.2.2 build 3211 似乎已修复:当我打开 test.py 时包含:

print("1")
print("á")
print("2")

然后用CTRL+B(用Python3.6)构建时,就正常了,out of -开箱即用。


备注:默认Python.sublime-build现在确实是:

{
    "shell_cmd": "python -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "python -m py_compile \"${file}\"",
        }
    ]
}