python2中进行Base64编码和解码:
import base64 a = base64.b64encode('780712') b = base64.b64decode(a) print(a) print(b)
输出结果:
NzgwNzEy
780712
python3不太一样:因为3.x中字符都为unicode编码,而b64encode函数的参数为byte类型,所以必须先转码。
import base64 a = base64.b64encode('780712'.encode(encoding='utf-8')).decode() b = base64.b64decode(a.encode("utf-8")).decode("utf-8") print(a) print(b)
输出结果:
NzgwNzEy
780712