参考链接: 在Python中编码和解码Base64字符串
首先,Base64生成的编码都是ascii字符。
其次,python3中字符都为unicode编码,而b64encode函数的参数为byte类型,所以必须先转码。
s = "你好"
bs = base64.b64encode(s.encode("utf-8")) # 将字符为unicode编码转换为utf-8编码
print(bs) # 得到的编码结果前带有 b
>>> b'5L2g5aW9'
bbs = str(base64.b64decode(bs), "utf-8")
print(bbs) # 解码
>>> 你好
bs = str(base64.b64encode(s.encode("utf-8")), "utf-8")
print(bs) # 去掉编码结果前的 b
>>> 5L2g5aW9
bbs = str(base64.b64decode(bs), "utf-8")
print(bbs) # 解码
>>> 你好