已解决AttributeError: ‘str’ object has no attribute 'decode’异常的正确解决方法,亲测有效!!!

已解决AttributeError: ‘str‘ object has no attribute ‘decode‘方案一_类型转换


文章目录

  • 报错问题
  • 解决方法
  • 声明


报错问题

粉丝群里面的一个小伙伴敲代码时发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错信息如下:

已解决AttributeError: ‘str‘ object has no attribute ‘decode‘方案一_python_02


根据问题提示,意思是,属性错误:“str”对象没有属性“decode”

python3.5和Python2.7在套接字返回值解码上的区别

python在bytes和str两种类型转换,所需要的函数依次是encode(),decode()

解决方法

解决方法如下

已解决AttributeError: ‘str‘ object has no attribute ‘decode‘方案一_解决方法_03


T1、直接去掉

直接去掉decode(‘utf8’)

tips:str通过encode()方法可以编码为指定的bytes。反过来,当从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法。反之,则使用encode()方法即可!

T2、众多网友好评的建议
谢谢 improve100 网友提供的建议!

pip install 'h5py<3.0.0' -i https://pypi.tuna.tsinghua.edu.cn/simple

声明

解决方法参考网络,如有侵权联系我删除