返回信息流python3是在官网直接下载安装的,pycharm配置project interpreter后一切正常
但配置sublime就出现了
1、print中文报错(通过csdn方法已解决)
2、open有中文的文件报错(求教解决方法。。)
目前是根据CSDN上的一个配置的http://blog.csdn.net/qq_33304418/article/details/63337602
sublime ,toos-->build system --> build new system
{
"cmd": ["/Library/Frameworks/Python.framework/Versions/3.6/bin/python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"env": {"PYTHONIOENCODING": "utf8"},
"selector": "source.python"
}
然后保存命名为Python3.sublime-build
之后设置
toos--build system--> automatic
python如下
# -*- coding: utf-8 -*-
with open('/Users/dario/Desktop/test.txt','r') as txtfile:
a=txtfile.read()
如果test.txt中有中文,command+b就会报错如下:
Traceback (most recent call last):
File "/Users/dario/Desktop/test.txt", line 4, in <module>
a=txtfile.read()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 8: ordinal not in range(128)
求大神教教怎么弄这个编码的问题?
一遇到编码的问题就晕。。。
这是一条镜像帖。来源:北邮人论坛 / python / #21178同步于 2018/3/18
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
mac的sublime中python3配置
qidi
2018/3/18镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
谢谢!确实加了encoding='utf-8'之后就正常了。
不过还是没明白原因,为啥pycharm就不用加呢?
# -*- coding: utf-8 -*-
with open('/Users/dario/Desktop/test.txt','r',encoding='utf-8') as txtfile:
a=txtfile.read()
【 在 intmain 的大作中提到: 】
: 根据你的文件编码为open函数添加encoding参数,比如encoding='utf-8'