将文档中标准系统输出的编码设置为UTF-8
:
import sys
import codecs
sys.stdout = codecs.getwriter( "utf-8" )( sys.stdout.detach() )
print( "1" )
print( "áéíóúý âêîôû äëïöü àèìòù ãñõ" )
print( "2" )
自动应用UTF-8
编码输出到所有文档,将前面的方法实现为内联command
在你的Python.sublime-build
file.
设置编码后,您的文档将通过以下方式加载exec
内联内command
.
{
"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}\"",
}
]
}
Tip: Use 包资源查看器创建一个用户副本 of Python.sublime-build
测试用Sublime Text 3
( 稳定通道,版本 3103 ) and Python
3.4.3
Sources:
如何在Python 3中设置sys.stdout编码?
Python 3 中 execfile 的替代品?