Sublime text 2/3 [Decode error - output not utf-8] 完美解決方法


原文鏈接

http://blog.csdn.net/bbdxf/article/details/25594703

[Decode error - output not utf-8]或者[Decode error - output not gbk]

 

錯誤信息意思就是腳本輸出的信息不是某種指定編碼.

指定的編碼一般在XX.sublime-build里,比如ruby.sublime-build的內容為: 

    {  
        "shell_cmd": "ruby \"$file\"",  
        "file_regex": "(\\w:...*?):([0-9]*):?([0-9]*)",  
        "selector": "source.ruby",  
        "encoding": "utf-8",   
    }  

 

我們可以通過修改ruby.sublime-build來修改輸出文字信息的編碼.

2.將ruby.sublime-build復制到sublime text的Data\Packages\User\目錄

以上這種修改有局限,比如我有時候輸出的是utf-8,有時候輸出的是gbk,這時候就不行了.

 

1.在sublime text的安裝目錄下的Packages\目錄下找到Default.sublime-package,將這個復制出來,將后綴改名為zip.

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  
      
         #add start  
         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})  

 



其原理就是在解碼輸出文字編碼出錯時再使用gbk試試,相當於utf-8和gbk兩種編碼都試試,這樣可以解決編碼錯誤的問題.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM