Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
import base64
str = "你好"
encodestr = base64.b64encode(str.encode('utf-8'))
print(encodestr)
b'5L2g5aW9' # 输出结果
# 可以看到,输出的结果被包含在 b' '中,表示byte字节,为了方便使用,我们使用下面的方法对其进行解码,直接得到字符串
encodestr2 = base64.b64encode(str.encode('utf-8')).decode('utf-8')
print(encodestr2)
5L2g5aW9 # 输出结果
decodestr = base64.b64decode(encodestr2.encode('utf-8'))
print(decodestr)
b'\xe4\xbd\xa0\xe5\xa5\xbd' # 输出结果
# 同理,我们使用下面的方法对其进行解码,直接得到字符串
decodestr2 = base64.b64decode(encodestr2.encode('utf-8')).decode('utf-8')
print(decodestr2)
你好 # 输出结果