分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

json,pickle模块

发布时间:2023-09-06 02:14责任编辑:蔡小小关键词:jsjson
序列化: dumps把内存的数据结构保存下来import jsondic={‘a‘:1}res1=json.dumps(dic)re2=str(dic)print(res1,type(res1)) ??#json格式都是用的双引号print(res2,type(res2)) ??x=Noneres=json.dumps(x)print(res,type(res))import json ?#把python格式转化为json格式user={‘name‘:‘xiaoxiao‘,‘age‘:18,‘ng‘:True} ?with open(‘user.json‘,‘w‘,encoding=‘utf-8‘) as f: ???f.write(json.dumps(user))import json ???#同上user={‘name‘:‘xiaoxiao‘,‘age‘:18,‘ng‘:True} json.dump(user,open(‘user_new.json‘,‘w‘,enccoding=‘utf-8‘)) ??#dump合并成一行反序列化:loadsimport json ?????with open(‘user.json‘,‘r‘,encoding=‘utf-8‘) as f: ???user=json.loads(f.read()) ??#以json格式读取文件内容 ???print(user.[‘name‘])import json u=json.load(open(‘user.json‘,‘r‘,encoding=‘utf-8‘)) ???#同上,load合并成一行print(u[‘age‘])json_res={"name":‘xi‘} ???#json只能用双引号print(json.load(json_res))pickle:序列化,能支持所有的Python类型,并以byte类型打开improt pickle,jsons={1,2,3,4}#print(json.dumps(s))print(pickle.dumps(s))with open(‘s.pkl‘,‘wb‘) as f: ???f.write(pickle.dumps(s))pickle.dump(s,open(‘s.pkl‘,‘wb‘)) #同上#pickle反序列化import picklewith open(‘s.pkl‘,‘rb‘) as f: ???s=pickle.loads(f.read()) ???print(s,type(s))s=pickle.load(open(‘s.pkl‘,‘rb‘)) ?#同上print(s,type(s))

json,pickle模块

原文地址:http://blog.51cto.com/13399294/2174483

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved