[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
*
AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?
原因:一般是因为 str 的类型本身不是 bytes,所以不能解码。
*
这是由于 py2 与 py3 字符串 编码上的区别 导致的。
# 这是 py2 的写法 name = "中国" name_utf8 = name.decode('utf-8') print(name, name_utf8
) # 输出:中国 中国 # 这是 py3 的写法 name = "中国" name_bytes = name.encode('utf-8')
name_utf8= name_bytes.decode('utf-8') print(name, name_bytes, name_utf8) #
输出:中国 b'\xe4\xb8\xad\xe5\x9b\xbd' 中国 # 不填使用默认编码格式 name = "中国" name_bytes = name.
encode() name_utf8 = name_bytes.decode() print(name, name_bytes, name_utf8) ##
概念: 1、普通 str:可理解的语义。 2、字节流 str(bytes):0101010101,字节类型其实就是 2 进制格式,
不过为了易于理解和计算,通常用 16 进制来表示, 也就是 b'\xe4\xb8\...,b 开头代表是 bytes 类型。 ## 语法
1、Encode(编码):把普通字符串 转为 机器可识别的 bytes。 2、Decode(解码):把bytes转为字符串。 ## 语法记忆小技巧 1、编码
encode,就是把你认识的转为机器认识的。 2、解码 decode,就是把机器认识的解释为人能读懂的。 ## 差异 1、py3 的 str 默认不是
bytes,所以不能 decode,只能先 encode 转为 bytes,再 decode。 2、py2 的 str 默认是 bytes,所以能
decode。 ## 结论 所以 str.decode 本质是 bytes 类型的 str 的 decode py3 经常出现 AttributeError:
‘str’ object has no attribute ‘decode’