分享web开发知识

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

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

json和pickle模块

发布时间:2023-09-06 01:49责任编辑:董明明关键词:jsjson
#json 模块输出的结果都是json字符串,可以被其他语言识别
import json
a = {‘name‘:‘alex‘} ???#------->{"name":"alex"}----------->‘{"name":"alex"}‘
b = 9 ?#---------->‘9‘
c = ‘hello‘ #-------->"hello" --------->‘"hello"‘
d = [11,22] #------‘[11,22]‘

print(json.dumps(a)) ?#将对象封装成json字符串
a = {‘name‘:‘liuhaiquan‘}
a_j = json.dumps(a)
with open(‘a.txt‘,‘r+‘) as f_read:
???f_read.write(a_j)

with open(‘a.txt‘,‘r‘) as f_read:
???data = f_read.read()
???print(json.loads(data)) ???#提取字符串内的字典
???print(type(json.loads(data)))
------------------------------------------


#pickle的用法和json一样,但pickle.dumps()处理的结果是字节 bytes
??读字节时需要用rb

但是pickle支持的数据类型更多,平时json用的多




json和pickle模块

原文地址:https://www.cnblogs.com/lhqlhq/p/8797015.html

知识推荐

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