摘要:sublime text有时运行会输出以下错误信息:[Decode error - output not utf-8]或者[Decode error - output not gbk],今天彻底解决这个问题。
sublime text有时运行会输出以下错误信息:
[Decode error - output not utf-8]或者[Decode error - output not gbk]
错误信息意思就是脚本输出的信息不是某种指定编码.
指定的编码一般在XX.sublime-build里,比如python.sublime-build的内容为:
{ "shell_cmd": "ruby \"$file\"", "file_regex": "(\\w:...*?):([0-9]*):?([0-9]*)", "selector": "source.ruby", "encoding": "utf-8", }
其中encoding就是指定的编码,python.sublime-build可以在Sublime Text3\Packages\python.sublime-package里找到.
我们可以通过修改python.sublime-build来修改输出文字信息的编码.
1. 将python.sublime-build文件从python.sublime-package里复制出来,(注意:python.sublime-package是一个zip压缩文件,只要将其后缀名改为zip即可打开这个压缩文件)
2.将python.sublime-build复制到sublime text的Data\Packages\User\目录
3. 打开此文件,将此文件中的"encoding": "utf-8"修改为"encoding": "gbk"
以上这种修改有局限,比如我有时候输出的是utf-8,有时候输出的是gbk,这时候就不行了.
以下方法可以解决这个局限.
1.在sublime text的安装目录下的Packages\目录下找到Default.sublime-package,将这个复制出来,将后缀改名为zip.
是的,它就是个zip文件,解压缩它,然后将其中的exec.py文件放到sublime text的Data\Packages\User\目录下.
2.打开exec.py.找到类ExecCommand的append_data函数,在以下位置添加代码
def append_data(self, proc, data): if proc != self.proc: # a second call to exec has been made before the first one # finished, ignore it instead of intermingling the output. if proc: proc.kill() return #从这开始添加修改 is_decode_ok = True; try: str = data.decode(self.encoding) except: is_decode_ok = False if is_decode_ok==False: try: str = data.decode("gbk") except: str = "[Decode error - output not " + self.encoding + " and gbk]\n" proc = None #修改结束 # Normalize newlines, Sublime Text always uses a single \n separator # in memory. str = str.replace('\r\n', '\n').replace('\r', '\n') self.output_view.run_command('append', {'characters': str, 'force': True, 'scroll_to_end': True})