Python报错'utf-8' codec can't decode bytes in position 1022-1023: unexpected end of data
Python执行是报错'utf-8' codec can't decode bytes in position 1022-1023: unexpected end of data...
网上查相关解决办法,都是添加如下,不靠谱都没解决
#!/usr/bin/env python # coding=utf-8
最后的解决办法添加
if function(r, 'decode'): # r = r.decode('utf-8') r = r.decode('utf-8','ignore')
官方解释如下
help("".decode) decode(...) S.decode([encoding[,errors]]) -> object Decodes S using the codec registered for encoding. encoding defaults to the default encoding. errors may be given to set a different error handling scheme. Default is 'strict' meaning that encoding errors raise a UnicodeDecodeError. Other possible values are 'ignore' and 'replace' as well as any other name registered with codecs.register_error that is able to handle UnicodeDecodeErrors.
其他参数例子
#decode解码的四个参数 print((b"\x80abc").decode("utf-8","strict"))#出现错误 #加上(U+FFFD) print((b"\x80abc").decode("utf-8","replace")) #加反斜杠 print((b"\x80abc").decode("utf-8","backslashreplace")) #直接忽略 print((b"\x80abc").decode("utf-8","ignore"))
参考
(39条消息) pycharm UnicodeDecodeError: ‘utf-8‘ codec can‘t decode bytes in position 1022-1023: unexpected end o_hllyzms的博客-CSDN博客
https://blog.csdn.net/weixin_37989267/article/details/124190568
(39条消息) python utf-8报错_python处理一些乱码的中文文本时decode('utf-8')报错的处理_weixin_39801158的博客-CSDN博客
https://blog.csdn.net/weixin_39801158/article/details/110696370
(39条消息) python编解码,decode参数设置:ignore_Yang.Young的博客-CSDN博客_ignore python
https://blog.csdn.net/symayy/article/details/107250441
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2016-07-08 MySQL架构优化:定时计划任务与表分区