python2中进行Base64编码和解码
>>> import base64 >>> s = '我是字符串' >>> a = base64.b64encode(s) >>> print a ztLKx9fWt/u0rg== >>> print base64.b64decode(a) 我是字符串
python3不太一样:因为3.x中字符都为unicode编码,而b64encode函数的参数为byte类型,所以必须先转码。
import base64 encodestr = base64.b64encode('abcr34r344r'.encode('utf-8')) print(str(encodestr,'utf-8'))
打印结果为
YWJjcjM0cjM0NHI=
参考:
https://www.cnblogs.com/zanjiahaoge666/p/7242642.html