简体   繁体   中英

Sublime Text 3, Python 3 and UTF-8 don't like each other

I am trying to parse an HTML page using a Python script I run in Sublime Text. This page contains non-ASCII characters. I kept getting [Decode error - output not utf-8] so I investigated a little and came down to this funny code snippet:

import codecs

#print((1, codecs.decode(codecs.encode('ò', 'utf-8'), 'utf-8')))
print('ò')

which prints [Decode error - output not utf-8] . This error does not happen if I encode an ASCII character. It is not a compile error - the program runs and completes - so I suspect this is a problem with Sublime Text processing the script output, but I can't narrow it down any further. How do I make this work?

Go ahead and open the command prompt and type in:

c:\>chcp

to check the encoding of the command. By me it's

Active code page: 852

Now open the Python.sublime-build ( C:\\Users\\User\\AppData\\Roaming\\Sublime Text 2\\Packages\\User ) build system and add the encoding:

{
    "cmd": ["c:\\python33\\python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "encoding": "cp852"
} 

This should cut it.

I am having the same problem with python 3.4 and Sublime Text 2. Even explicitly stating the encoding of the code (with # - - coding: UTF-8 - - at the very first line of the file) did not help. I have also tried explicitly encoding the offending string(s), to no avail.

This seems to be a sublime internal problem/bug as I have also tried to encode the source document of the offending parts (in notepad++ encoded to UTF-8 without BOM) with the same result.

While I do not know how to fix the problem within Sublime 2/3, I can only offer the work-around ==> use CMD/PowerShell, which works perfectly fine with correct output for me.

PS: I hope this helps but please note that I am neither an experienced programmer nor have a deep knowledge of python or sublime. I just wanted to contribute my 2 cents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM