Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

utf 8 - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:UsersUserAppDataRoamingSublime Text 2PackagesUser) 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...