首先,Base64生成的編碼都是ascii字符。python
其次,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) # 解碼>>> 你好