Python报错'utf-8' codec can't decode bytes in position 1022-1023: unexpected end of dat

2022/7/9 1:21:32

本文主要是介绍Python报错'utf-8' codec can't decode bytes in position 1022-1023: unexpected end of dat,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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



这篇关于Python报错'utf-8' codec can't decode bytes in position 1022-1023: unexpected end of dat的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程