Python base64编码和解码

首先在Python2中base的编码和解码

1 s = "我是一个字符串"
2 # 编码
3 base64.b64encode(s)
4 # 解码
5 base64.b64decode(s)

在Python3中和Python2中是稍微有一点区别的。

1 s = "我是一个字符串"
2 # 编码
3 base64.b64encode(s.encode("utf-8"))
4 # 解码
5 a = base64.b64decode(s)
6 str(a, "utf-8")

外加上base64的验证方法

 1 res = re.match("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$",str(a,"utf-8")) 

原文地址:https://www.cnblogs.com/shangwei/p/13937739.html