当前位置:网站首页>出现UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe9 in position 0: ordinal not in range解决方法
出现UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe9 in position 0: ordinal not in range解决方法
2022-06-22 00:50:00 【码农研究僧】
1. 问题所示
在传输数据的时候
出现这个问题,如下所示:
File "./audioadmin/common.py", line 331, in send_alarm
.format(content, project_name, result))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
截图如下:
2. 问题分析
python2.7版本:(项目中使用的2版本):
python的编码转换会以unicode作为中间转码(unicode只有128的长度)
编码的过程都是先将其Ascii编码字符(默认编码方式)转换成unicode
通过上面的日志可得知无法转码,超出了范围,就报这种错误日志
本身编码的时候sys.defaultencoding默认是Ascii,而传入的编码是utf-8,格式不一样,自然就报错了
python3版本以上已经兼容了这种问题
测试python3 兼容的问题:
import sys
sys.getdefaultencoding() # 输出utf-8,而在python2.7中会输出Ascii
3. 解决方法
在项目中的开头引入:
import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
边栏推荐
- What does container cloud mean? What is the difference with fortress machine?
- Sed technique
- Error 4 opening dom ASM/Self in 0x8283c00
- Use gomonkey mock function and method
- Standing at the digital tuyere, how can tooling enterprises "fly"
- Mysql database high version low version
- 3分钟,带你玩转聊天机器人自动化【顶级模板】
- Panic: permission denied problems encountered when using gomonkey mock functions and methods and Solutions
- [cyw20189] VII. Detailed explanation of HCI command format
- [noi simulation] interval distance (block and convolution)
猜你喜欢
![[ÑÖÏ Simulation Competition] fading (matrix acceleration, cyclic convolution, Gauss elimination)](/img/4a/9dfcb699e36f67e14c036e3ae26417.png)
[ÑÖÏ Simulation Competition] fading (matrix acceleration, cyclic convolution, Gauss elimination)

【ÑÖÏ模拟赛】花萎(矩阵加速,循环卷积,高斯消元)

Idea prompt duplicated code fragment (15 lines long)

Using SSM framework to realize user login

对标Copilot,国内首个:自然语言一键生成方法级代码aiXcoder XL来了

php-admin部署-解决全部错误

SAP MM 进口采购业务中供应商多送或者少送场景的处理

内网学习笔记(3)

Huawei cloud releases desktop ide codearts

将列表分箱,并通过Pyechart绘制柱状图
随机推荐
SparkRDD 案例:计算总成绩
[解决方案] 明厨亮灶视频边缘计算网关解决方案
[gstreamer] plug in writing - Test Program
Yolov3 3D semantic point cloud paper reading
判断String类型是否为空,判断list集合是否为空
ShardingSphere-proxy-5.0.0分布式哈希取模分片实现(四)
今日内容
Conversion between three file handles
杨冰:OceanBase助力数字化转型,原生分布式数据库成核心系统首选
The use of PowerShell functions in mathematical expressions
Judge whether the string type is empty and whether the list set is empty
Show you how to distinguish several kinds of parallelism
Difference between MSVC and GCC (under collection)
How to remove duplication in left join from a simple example
field.setAccessible(true);代码扫描有安全漏洞,解决方案
【NOI模拟赛】区间距离(分块,卷积)
Find find files with different extensions
Promise me not to use if (obj! = null) to judge empty
【数论】leetcode1010. Pairs of Songs With Total Durations Divisible by 60
Dynamic programming-01 backpack, partition, etc. and subset, weight of the last stone