zoukankan      html  css  js  c++  java
  • python3提示:AttributeError: ‘str’ object has no attribute ‘decode’

    AttributeError: ‘str’ object has no attribute ‘decode’
    一般是因为str的类型本身不是bytes,所以不能解码

    两个概念:
    普通str:可理解的语义
    字节流str(bytes)(0101010101,可视化显示)

    两个语法
    Encode: 把普通字符串 转为 机器可识别的bytes
    Decode: 把bytes转为字符串

    两个差异
    python3的str 默认不是bytes,所以不能decode,只能先encode转为bytes,再decode
    python2的str 默认是bytes,所以能decode

    一个结论
    所以str.decode 本质是bytes类型的str的decode
    python3经常出现 AttributeError: ‘str’ object has no attribute ‘decode’

    非要这样玩,只能先encode转为bytes,再decode
    强制转换忽略错误:
    bytes.decode(‘’utf-8‘’, ‘’ignore‘’)

    常用解决方法:
    print (‘张俊’.encode(‘utf-8’). decode(‘utf-8’) ) #必须将字节字符串解码后才能打印出来

    ————————————————
    版权声明:本文为CSDN博主「柠 檬没我萌」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_43192819/article/details/108981008

  • 相关阅读:
    POJ-1182 食物链
    hdu 1879 继续畅通工程
    HDU 2604 Queuing
    hdu 1232 畅通工程
    POJ-1611 The Suspects
    Free DIY Tour
    Tr A
    不容易系列之(3)―― LELE的RPG难题
    W3C标准冒泡、捕获机制
    JavaScript 浏览器事件解读
  • 原文地址:https://www.cnblogs.com/peter-jun/p/14865622.html
Copyright © 2011-2022 走看看