Sublime报Decode error - output not utf-8 or cp936 错误的解决办法

来源:互联网 发布:淘宝轮毂店 编辑:程序博客网 时间:2024/06/05 13:43
使用Sublime在build python程序时,有时候会遇到Decode error -output not utf-8或者是cp936。原因是python编译运行后的流的编码方式和Sublime的解码方式不同,Sublime Ctrl+B build一个python程序时,输出output not cp936,说明Sublime中的Python build默认接收的编码是cp936,如果你的python 程序是utf-8编码,有汉字输出时,Sublime就会报not cp936的错误。
如果报的是output not utf-8,说明python在编译运行源码后默认将输出结果以cp936编码发送给Sublime。而Sublime中的默认接收编码是utf-8。Sublime Text在接收到python输出后,试图以utf-8编码cp936编码的流。当cp936编码的流中没有中文字符时,用utf-8解码不会出错,当cp936编码的流中有中文字符时,因为汉子集在cp936与utf-8的编码不兼容,所以用utf-8解码就会报错。
解决问题的关键就是弄明白你的python程序的编码和Sublime的python  build中指定的编码。
python   sublime     decode error
cp936    utf-8         output not utf-8
utf-8      cp936       output not cp936
比如,你的python代码中制定了编码格式是utf-8,现在sublime报错了output not cp936的错误,则设置Sublime中python build编码格式为utf-8,反之亦然。
Python.sublime-build 位置:
Sublime  Text 2 : SublimeText2/Data/Packages/Python/ Python.sublime-build
Sublime Text 3 :SublimeText3/Packages/Python.sublime-package 
或者是新建一个build文件:工具->编译系统->新建编译系统,保存为Python.sublime-build,我的文件是保存在了C:\Program Files (x86)\Sublime Text\Data\Packages\User\ 中。
{
    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "encoding":"utf-8"
}

参考:
http://www.th7.cn/Program/Python/201411/319372.shtml
http://blog.csdn.net/wanzhuan2010/article/details/19168709
1 0
原创粉丝点击