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

posted @ 2022-07-08 13:51  paul_hch  阅读(1970)  评论(0编辑  收藏  举报