python3与python2相比,base64编码和解码时的参数类型有所变化。
python2中base64.b64encode()的参数为str,python3中由于为unicode编码,参数改为byte类型,因此要对参数先进行encode()方法,该方法返回编码后的字符串,它是一个 bytes 对象。
同样的,base64.b64decode()得到的结果也为byte类型,对其进行操作可以用decode()方法。
s='你在哪'
>>> s.encode()
b'\xe4\xbd\xa0\xe5\x9c\xa8\xe5\x93\xaa'
>>> base64.b64encode(s.encode())
b'5L2g5Zyo5ZOq'
>>> b=base64.b64encode(s.encode())
>>> base64.b64decode(b)
b'\xe4\xbd\xa0\xe5\x9c\xa8\xe5\x93\xaa'
>>> base64.b64decode(b).decode()
'你在哪'