import base64
s='hello world'
bytes_by_s=s.encode() #将字符串编码-->字节码,
b64_encode_bytes=base64.b64encode(bytes_by_s) #base64编码
print(b64_encode_bytes)
b64_decode_bytes=base64.b64decode(b64_encode_bytes) #base64解码
print(b64_decode_bytes)
s_by_bytes=b64_decode_bytes.decode() #字节码解码-->字符串
print(s_by_bytes)
输出:
b'aGVsbG8gd29ybGQ='
b'hello world'
hello world
base64更改编码表:
mg_base= "tuvwxTUlmnopqrs7YZabcdefghij8yz0123456VWXkABCDEFGHIJKLMNOPQRS9+/="
std_base= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
trantab = str.maketrans(std_base, mg_base) # 制作翻译表
import base64
s='hello world'
bytes_by_s=s.encode()
b64_encode_bytes=base64.b64encode(bytes_by_s)
print(b64_encode_bytes)
mgs=b64_encode_bytes.decode().translate(trantab)
print(mgs)
输出:
b'aGVsbG8gd29ybGQ='
iUdCjUS1yM9IjUY=
本文详细介绍了如何使用Python进行Base64编码和解码操作,包括字符串到字节码的转换,以及如何修改Base64的编码表实现自定义编码。通过实例展示了Base64编码的基本用法。

3348

被折叠的 条评论
为什么被折叠?



