python中base64编码与解码(将json串转化成base64)

【python中base64编码与解码(将json串转化成base64)】python中base64编码与解码(将json串转化成base64)
单个字段转换
#- * - coding: utf - 8 -
import requests
import base64
import json
#json串转换成base64(转码)
encodestr = base64.b64encode(‘43048e640910498e94023893c4bb5320’.encode(‘utf-8’))
print(encodestr)
print(str(encodestr,‘utf-8’))
#base64转换成json(解码)
ww=base64.b64decode(encodestr)
print(str(ww,‘utf-8’))
python中base64编码与解码(将json串转化成base64)
文章图片

从文件中转换
#- * - coding: utf - 8 -
import base64
def main():
# file 文件类型的对象
#open(‘1.txt’,encoding=’gb18030’,errors=‘ignore’)
#with open(r’F:\aa.txt’, encoding=“utf-8”) as file:
#with open(‘F:\aa.txt’,‘rb’,encoding=‘gb18030’,errors=‘ignore’,) as file:
with open(‘F:\aa.txt’,‘rb’) as file:
#print(type(file))
#print(file)
# 以列表的形式输出文本
lines = list( file)
#print(lines)
# 输出文本的每一行
for eachLine in lines:
encodestr1= base64.b64encode(eachLine)
print(str(encodestr1, ‘utf-8’))
if name == ‘main’:
main()
#inputRead3 = open(‘F:\aa.txt’, ‘rb’)
#encodestr1 = base64.b64encode(inputRead3.read())
#print(str(encodestr1, ‘utf-8’))
python中base64编码与解码(将json串转化成base64)
文章图片

    推荐阅读