今天在用base64转码时,报错,提示:TypeError: a bytes-like object is required, not 'str’如下图
报错原因:python2和python3对套接字返回值解码上有区别
处理方法:解决办法非常的简单,只需要用上python的bytes和str两种类型转换的函数encode()、decode()即可!
str通过encode()方法可以编码为指定的bytes;
反过来,要把bytes变为str,就需要用decode()方法.(比如我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes)
把代码改为这样就可以了:
def cd6401(self):
base_str = ‘liuyuan123’
ss = base64.b64encode(base_str.encode(encoding=“utf-8”))
print(ss)
de_str=base64.b64decode(ss.decode())
print(de_str)
7829

被折叠的 条评论
为什么被折叠?



