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’))
在这里插入图片描述
从文件中转换
#- * - 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’))

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值