python

python3中base64的用法是什么

小亿
171
2024-04-03 15:33:57
栏目: 编程语言

在Python中,可以使用base64模块来对数据进行base64编码和解码。base64模块提供了多个方法来实现这些功能,以下是一些常用的方法:

  1. base64.b64encode(data):对data进行base64编码。
  2. base64.b64decode(s):对base64编码的字符串s进行解码。
  3. base64.urlsafe_b64encode(data):对data进行URL安全的base64编码。
  4. 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个字符。

0
看了该问题的人还看了