'ascii' codec can't decode byte 0xef in position 0:ordinal not in range(128)错误解决与原理分析
来源:互联网 发布:迅雷已死 知乎 编辑:程序博客网 时间:2024/06/06 18:52
写python代码时出现’ascii’ codec can’t decode byte 0xef in position 0:ordinal not in range(128)的错误。
在解决错误之前,首先要了解unicode和utf-8的区别。
unicode指的是万国码,是一种“字码表”。而utf-8是这种字码表储存的编码方法。unicode不一定要由utf-8这种方式编成bytecode储存,也可以使用utf-16,utf-7等其他方式。目前大多都以utf-8的方式来变成bytecode。
其次,Python中字符串类型分为byte string 和 unicode string两种。
如果在python文件中指定编码方式为utf-8(#coding=utf-8),那么所有带中文的字符串都会被认为是utf-8编码的byte string(例如:mystr=”你好”),但是在函数中所产生的字符串则被认为是unicode string。
问题就出在这边,unicode string 和 byte string 是不可以混合使用的,一旦混合使用了,就会产生这样的错误。例如:
self.response.out.write(“你好”+self.request.get(“argu”))
其中,”你好”被认为是byte string,而self.request.get(“argu”)的返回值被认为是unicode string。由于预设的解码器是ascii,所以就不能识别中文byte string。然后就报错了。
以下有两个解决方法: 1.将字符串全都转成byte string。 self.response.out.write("你好"+self.request.get("argu").encode("utf-8")) 2.将字符串全都转成unicode string。 self.response.out.write(u"你好"+self.request.get("argu")) byte string转换成unicode string可以这样转unicode(unicodestring, "utf-8")
最后再补充点,出现报错后,将byte string转化为Unicode string的方法
apply_content = unicode(dict[‘apply_content’], ‘utf-8’)
直接将str变成unicode
另外可以用type(string)检查字符串的类型是Unicode还是str类型
- 'ascii' codec can't decode byte 0xef in position 0:ordinal not in range(128)错误解决与原理分析
- 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
- ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 10: ordinal not in range(128)
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 458: ordinal not in range(128)
- Pyhotn 中编码的问题:'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)的错误
- Pyhotn 中编码的问题:'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)的错误
- Pyhotn 中编码的问题:'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)的错误
- 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
- ascii codec can't decode byte 0xe8 in position 0:ordinal not in range(128)
- ascii codec can't decode byte 0xe8 in position 0:ordinal not in range(128)
- 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
- 'ascii' codec can't decode byte 0xc4 in position 27: ordinal not in range(128)
- 'ascii' codec can't decode byte 0x** in position **, ordinal not in range(128)
- 【Python错误】 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)错误解
- 'ascii' codec can't decode byte 0xe7 in position 12: ordinal not in range(128)的错误
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
- No dashboards are active for the current data set. 解决tensorboard无法启动和显示问题(pycharm启动)
- requests.get为什么得到的内容和查看源文件不一样?
- yum install 提示 except OSError, e: ^ SyntaxError: invalid syntax
- 服务器数据库等杂项
- linux下tomcat,jdk完整安装
- 'ascii' codec can't decode byte 0xef in position 0:ordinal not in range(128)错误解决与原理分析
- CentOs 安装java环境
- Spring Cloud (12) | Spring Cloud Zuul网关调用微服务,request请求参数是application/json
- 欢迎使用CSDN-markdown编辑器
- 伪类清浮动
- 工作方式的思考
- ptotobuf grpc java端通信
- do-while
- 新型电源 IC让无线医疗仪表受益---凯利讯半导体