共計 520 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Python 中,可以使用 base64 模塊來對數據進行 base64 編碼和解碼。base64 模塊提供了多個方法來實現這些功能,以下是一些常用的方法:
- base64.b64encode(data):對 data 進行 base64 編碼。
- base64.b64decode(s):對 base64 編碼的字符串 s 進行解碼。
- base64.urlsafe_b64encode(data):對 data 進行 URL 安全的 base64 編碼。
- base64.urlsafe_b64decode(s):對 URL 安全的 base64 編碼的字符串 s 進行解碼。
示例代碼:
import base64
# 對字符串進行 base64 編碼
data = b'hello world'
encoded_data = base64.b64encode(data)
print(encoded_data)
# 對 base64 編碼的字符串進行解碼
decoded_data = base64.b64decode(encoded_data)
print(decoded_data)
注意:base64 編碼后的字符串會比原始數據稍長,因為 base64 編碼是將 3 個字節的數據轉換為 4 個字符。
丸趣 TV 網 – 提供最優質的資源集合!
正文完