Mac sublime 编译Python UnicodeEncodeError: 'ascii' codec can't encode characters in position 6-8: ordinal not in range(128)
刚学Python,想打印个“hello 张林峰”,代码如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*- print('hello 张林峰')
用sublime运行一下,竟然报错???
Traceback (most recent call last):
File "/Users/zhanglinfeng/Documents/Python/\u7ec3\u4e60/\u5b57\u7b26\u4e32\u7f16\u7801\u53ca\u8f93\u51fa\u683c\u5f0f.py", line 4, in <module>
print('hello \u5f20\u6797\u5cf0')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 6-8: ordinal not in range(128)
[Finished in 0.1s with exit code 1]
[cmd: ['/Library/Frameworks/Python.framework/Versions/3.6/bin/python3', '-u', '/Users/zhanglinfeng/Documents/Python/练习/字符串编码及输出格式.py']]
[dir: /Users/zhanglinfeng/Documents/Python/练习]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
可我明明写了# -*- coding: utf-8 -*-的啊。去终端运行下这个py文件,特么终端竟然对的,可以打印hello 张林峰了。什么情况啊?
网上搜了下,哟呵,报这个错的人还挺多啊。大概有以下几种答案:
1.是因为默认的编码是ascii,要把需要自己将python的默认编码设置为utf8的编码格式
查询系统默认编码可以在解释器中输入以下命令:
>>>import sys
>>>sys.getdefaultencoding()
设置默认编码时使用:
>>>import sys
>>>reload(sys)
>>>sys.setdefaultencoding('utf8')
这个方法在终端上运行Python时有用的,然而在sublime上还是不行。
2.认为是print的的局限性
需要在前面加上下面代码
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码
我的就是第二个答案解决的。注意:这里有一个坑,我已经设置sublime运行3.6版本的Python,用SublimeREPL插件进行交互式运行时,竟然还是2.7。用2.7版本的Python时sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')会报错。所以还要对SublimeREPL设置Python版本。这个卡了我好久,曾一度认为网上答案都是错的,只是网上答案不全而已,可能遇到的坑都没说。
Mac sublime 编译Python UnicodeEncodeError: 'ascii' codec can't encode characters in position 6-8: ordinal not in range(128)的更多相关文章
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-5: ordin al not in range(128)——解决方案备注
在vim中使用ycm插件时,偶尔会出现: “UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-5: ord ...
- [转]Python UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 的解决办法
UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 的解决办法 python在安装时,默认的编码是ascii,当 ...
- Python UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
#!/usr/bin/python# -*- coding: utf-8 -*- 解决方法: 可以看到我的版本是2.6的,所以打开/usr/lib64/python2.6/site.py 红框里本来是 ...
- Python编码问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(12
今天安装了PyScripter编辑器,刚要写代码,突然就出现异常: <span style="font-size:14px;color:#ff0000;">>&g ...
- python+selenium运行报错UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
使用python+selenium运行自动化脚本时,打印某一段文字出现UnicodeEncodeError: 'ascii' codec can't encode characters in posi ...
- 【python问题】UnicodeEncodeError: 'ascii' codec can't encode characters in position 306-309: ordinal not in range(128)
今天在写python爬虫的时候,遇到一个问题 UnicodeEncodeError: 'ascii' codec can't encode characters in position 306-309 ...
- python输出字符串,UnicodeEncodeError: 'ascii' codec can't encode characters in position问题
2017-06-28更新:换到python3.x中,编码问题减少了很多.这篇博文不适用于python3.x http://blog.sina.com.cn/s/blog_64a3795a01018vy ...
- 【Python】UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3
问题如下: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3 解决方法: 程序开始加上下面两句 sys ...
- python:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
# 将默认编码设为utf-8 # 否则会报错: # UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ...
随机推荐
- Web应用程序开发,基于Ajax技术的JavaScript树形控件
感谢http://www.cnblogs.com/dgrew/p/3181769.html#undefined 在Web应用程序开发领域,基于Ajax技术的JavaScript树形控件已经被广泛使用, ...
- LeetCode Regular Expression Matching 网上一个不错的实现(非递归)
'.' Matches any single character.'*' Matches zero or more of the preceding element. The matching sho ...
- CTL_CODE说明
DeviceIoControl函数的第二个参数IoControlCode就是由CTL_CODE宏定义的,下边我们可以了解一下CTL_CODE的内容. CTL_CODE:用于创建一个唯一的32位系统I/ ...
- HDU1142 (Dijkstra+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- 算法题之Leetcode分糖果
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
- 扩展 RequestHandlerBase
RequestHandlerBase 实现接口SolrRequestHandler SearchHandler: 它的所有逻辑来自 搜索组件SearchComponents. handler配置中, ...
- k8s的DamonSet使用
Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本.DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本. 1.Daemo ...
- MYSQL 的异常CRASH事件处理
检查问题的过程****************************************************************************************** ps ...
- css行高line-height的用法
一.line-height语法 line-height属性的具体定义列表如下: 语法: line-height : normal | <实数> | <长度> | <百分比 ...
- (十六)MySQL集群galera实现
(1)环境介绍 galera官网:http://galeracluster.com/downloads/ # cat /etc/redhat-release CentOS Linux release ...