【转】Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1
转自:https://blog.csdn.net/AckClinkz/article/details/78538462
环境
>>> import sys
>>> print(sys.version)
'3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 12:22:00) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'
- 1
- 2
- 3
问题描述
今天在使用python3的时候,报错信息
Traceback (most recent call last):
File "tmp.py", line 3, in <module>
print(a)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
- 1
- 2
- 3
- 4
报错代码可简化为
a = b'\xe5\x94\xb1\xe6\xad\x8c'
a = a.decode("utf-8")
print(a)
- 1
- 2
- 3
问题分析
本节介绍问题的分析过程,如果想看解决办法,可以直接看一下节。
网上解释
网上给出的解释:错误的使用decode和encode方法会出现这种异常。例如使用decode方法将Unicode字符串转化的时候:
s = u'中文'
s.decode('utf-8')
print s
- 1
- 2
- 3
但是将这个例子放到python3环境中,会报错
Traceback (most recent call last):
File "tmp_2.py", line 4, in <module>
s.decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'
- 1
- 2
- 3
- 4
熟悉python历史的朋友会知道,为了解决编码问题,在python3中,所有的字符串都是使用Unicode编码,统一使用str类型来保存,而str类型没有decode方法,所以网上给出的方向并不适合我的问题。
字符编码
为了确定是否是字符编码的问题,我换了一台python3机器,测试了一下
>>>a = b'\xe5\x94\xb1\xe6\xad\x8c'
>>>a = a.decode("utf-8")
>>>print(a)
唱歌
- 1
- 2
- 3
- 4
完全没有问题,正常输出,排除字符编码和代码失误。
输出
既然字符编码、代码都没有错,那么问题肯定出在print上面。这时我开始关注错误信息中的ascii。因为在一般python3环境中,输出时会将Unicode转化为utf-8。为了解开这个疑惑,查看了输出编码
>>>import sys
>>>sys.stdout.encoding
'ANSI_X3.4-1968'
- 1
- 2
- 3
竟然是ANSI_X3.4-1968,所以任何中文都会报错。哈哈,终于定位问题啦。
解决方案
定位问题后,解决办法就很简单啦,有两种方法
运行python的时候加上PYTHONIOENCODING=utf-8,即
PYTHONIOENCODING=utf-8 python your_script.py
- 1
- 重新定义标准输出
标准输出的定义如下
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
- 1
打印日志的方法
sys.stdout.write("Your content....")
- 1
总结
通过分析这个问题,进一步加深了对python3的了解。另外,希望各位看官批评指正!!
【转】Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1的更多相关文章
- (转) Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1
(转)python(三):Python3-UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1 python ...
- python爬虫中对含中文的url处理以 及 Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position
在练习urllib操作中,遇到了url中含有中文字符的问题.比如http://dotamax.com/,看下源码的话,上方的搜索框的name=p,输入内容点击搜索以后,通过GET方法进行传递,比如我们 ...
- 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 ...
- 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 张林峰') 用su ...
- 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 ...
- Pip 安装 出现UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in
在Python 环境下,使用PiP 命令安装时,报错提示: UnicodeEncodeError: 'ascii' codec can't encode characters in position ...
- 解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 问题(转)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-11: ordinal not in range(128 ...
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 14-15: ordinal not in range(128)
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报类似这样的错误. UnicodeEncodeError: 'ascii' codec can't ...
随机推荐
- (light oj 1024) Eid (最小公倍数)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1024 In a strange planet there are n races. ...
- SpringMVC 框架介绍以及环境搭建
目录 前端设计模式介绍 分析前端设计模式 Spring MVC简单介绍 Spring和Spring MVC的关系 配置Spring MVC的环境并简单测试 前端设计模式介绍 前端设计模式其实和前端没啥 ...
- .netcore 读取ansi编码
public class FileHelper { //根据文件自动觉察编码并输出内容 public static string GetText(string path) { StringBuilde ...
- Virtual DOM 系列二:核心API
为了更好的研究Virtual DOM,我选择了snabbdom来学习.相比Vue来说,snabbdom对于研究虚拟DOM更好,因为它里面没有其他干扰的东西,而且源码也比较少,因此研究起来更方便. 1. ...
- vue-cli3相关
此时做的一个vue-cli3项目build后,app.js达到了10M,主要为elementui.quill等组件: 最开始使用“compression-webpack-plugin”插件根据网上的说 ...
- 2019春招面试高频题(Java版),持续更新(答案来自互联网)
第一模块--并发与多线程 Java多线程方法: 实现Runnable接口, 继承thread类, 使用线程池 操作系统层面的进程与线程(对JAVA多线程和高并发有了解吗?) 计算机资源=存储资源+计算 ...
- es6中常用方法
查询数组中是否包含了某个元素keyword arr.includes(keyword)
- .Net Core实践3 配置文件
环境 .netcore2.1 / vs2017 / win10 / centos7 在.netcore项目中读取配置文件,先添加应用程序配置文件App.config.这个是类库项目的配置文件名. Sy ...
- MySQL架构备份之M-S-S级联备份
M--S1--S2 级联复制 master—>slave1—>slave2 master需要开启二进制日志 中间的slave1也需要打开二进制日志,但是它默认不把应用master的操作记录 ...
- 将字符串存储到注册表中,长度一定是 strlen(text) + 1
参考:https://docs.microsoft.com/en-us/windows/desktop/sysinfo/registry-value-types 将字符串存储到注册表中,长度参数一定要 ...