网站推广优化怎样,个人网站免费模板下载,黑龙省建设厅网站,简单个人网站一、Python的标准库
base64模块是用来作base64编码解码#xff0c;常用于小型数据的传输。编码后的数据是一个字符串#xff0c;其包括a-z、A-Z、0-9、/、共64个字符#xff0c;即可用6个字节表示#xff0c;写出数值就是0-63.故三个字节编码的话就变成了4个字节#xff…一、Python的标准库
base64模块是用来作base64编码解码常用于小型数据的传输。编码后的数据是一个字符串其包括a-z、A-Z、0-9、/、共64个字符即可用6个字节表示写出数值就是0-63.故三个字节编码的话就变成了4个字节如果数据字节数不是3的倍数就不能精确地划分6位的块此时需要在原数据后添加1个或2个零值字节使其字节数为3的倍数然后在编码后的字符串后添加1个或2个‘’表示零值字节所以事实上总共由65个字符组成
二、常用方法
base64.b64encode(str, altcharsNone)用于对字节字符串进行Base64编码。
str字节字符串altchars替代字符集用于替换Base64编码中的“和”/字符。
base64.b64decode(str, altcharsNone, validateFalse)用于对Base64编码的字节字符串进行解码。
str字节字符串altchars替代字符集用于替换Base64编码中的“和”/字符。validate用于指定是否进行解码前的验证
import base64org_str hello wordencode_str base64.b64encode(org_str.encode())
decode_str base64.b64decode(encode_str)print(原字符串, org_str)
print(编码后的字符串, encode_str)
print(解码后的字符串, decode_str.decode())
base64.a85encode(str, foldspacesTrue, wrapcol0, padFalse, adobeFalse)用于对字节字符串进行Ascii85编码。
str字节字符串foldspaces是否折叠空格。wrapco每行的字符数限制。pad是否在编码后添加填充字符。adobe是否使用Adobe的Ascii85编码变体。
base64.a85decode(str, foldspacesTrue, adobeFalse, ignorechars’ \t\n\r\x0b’)用于对Ascii85编码的字节字符串进行解码。
str字节字符串foldspaces是否折叠空格。adobe是否使用Adobe的Ascii85编码变体。ignorechars要忽略的字符集。 import base64
import base64org_str hello wordencode_str base64.a85encode(org_str.encode())
decode_str base64.a85decode(encode_str)print(原字符串, org_str)
print(编码后的字符串, encode_str)
print(解码后的字符串, decode_str.decode())base64.b16encode(str)用于对字节字符串进行十六进制编码。base64.b16decode(str, casefoldFalse)用于对十六进制编码的字节字符串进行解码。
str字节字符串casefold是否将解码后的结果转换为小写。
import base64org_str hello wordencode_str base64.b16encode(org_str.encode())
decode_str base64.b16decode(encode_str)print(原字符串, org_str)
print(编码后的字符串, encode_str)
print(解码后的字符串, decode_str.decode())base64.b32encode(str)用于对字节字符串进行Base32编码。base64.b32decode(str, casefoldFalse, map01None)用于对Base32编码的字节字符串进行解码。
str字节字符串casefold是否将解码后的结果转换为小写。map01自定义的Base32字符映射。
import base64org_str hello wordencode_str base64.b32encode(org_str.encode())
decode_str base64.b32decode(encode_str)print(原字符串, org_str)
print(编码后的字符串, encode_str)
print(解码后的字符串, decode_str.decode())
base64.b32hexencode(str)用于对字节字符串进行Base32hex编码。base64.b32hexdecode(str, casefoldFalse)用于对Base32hex编码的字节字符串进行解码。
str字节字符串casefold是否将解码后的结果转换为小写。
import base64org_str hello wordencode_str base64.b32hexencode(org_str.encode())
decode_str base64.b32hexdecode(encode_str)print(原字符串, org_str)
print(编码后的字符串, encode_str)
print(解码后的字符串, decode_str.decode())base64.b85decode(str)用于对Base85编码的字节字符串进行解码。base64.b85encode(str, padFalse)用于对字节字符串进行Base85编码。
str字节字符串pad是否在编码后添加填充字符。
import base64org_str hello wordencode_str base64.b85encode(org_str.encode())
decode_str base64.b85encode(encode_str)print(原字符串, org_str)
print(编码后的字符串, encode_str)
print(解码后的字符串, decode_str.decode())
base64.encodebytes(str)用于对Base64编码的字节字符串进行编码。base64.decodebytes(str)用于对Base64编码的字节字符串进行解码。
import base64org_str hello wordencode_str base64.encodebytes(org_str.encode())
decode_str base64.decodebytes(encode_str)print(原字符串, org_str)
print(编码后的字符串, encode_str)
print(解码后的字符串, decode_str.decode())
base64.standard_b64decode(str)用于对标准Base64编码的字节字符串进行解码。base64.standard_b64encode(str)用于对字节字符串进行标准Base64编码。base64.urlsafe_b64encode(str)用于对字节字符串进行URL安全的Base64编码。base64.urlsafe_b64decode(str)用于对URL安全的Base64编码的字节字符串进行解码。