sublime text 2在python3.3下输出中文字符的问题


在python2.7下,我在一个文件中输入了点中文字符,然后保存为txt格式文件,之后读取输入至控制台没有报错。但是切换至python3.3下,读取同样一个文件,就报错,提示:
/Users/nintenace/Desktop/learn_python
Traceback (most recent call last):
File "/Users/nintenace/Desktop/learn_python/test.py", line 7, in <module>
print(str)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
[Finished in 0.0s with exit code 1] </module>

代码如下:
with open('ttt.txt','r',encoding='utf-8') as ttt: str = ttt.readline() print(str)

很奇怪为什么会这样,我是在mac下操作的,望高手解答!

python3.x python

星辰的哀伤 11 years, 6 months ago

http://stackoverflow.com/a/15174760

Preferences -> Browse Packages -> Python -> Python.sublime-build

加上一句 env ,比如这样:


 {
    "cmd": ["/usr/local/bin/python3.3", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "env": {"LANG": "en_US.UTF-8"}
}

注意 selector 最后的逗号!

震爹一下巧克力 answered 11 years, 6 months ago

Your Answer