python 处理json
1、dumps:将python中的 字典 转换为 字符串
import json test_dict = {'bigberg': [7600, {1: [['iPhone', 6300], ['Bike', 800], ['shirt', 300]]}]} print(test_dict) print(type(test_dict)) #dumps 将数据转换成字符串 json_str = json.dumps(test_dict) print(json_str) print(type(json_str))
2、loads: 将 字符串 转换为 字典
new_dict = json.loads(json_str) print(new_dict) print(type(new_dict))
3、dump: 将数据写入json文件中
with open("../config/record.json","w") as f: json.dump(new_dict,f) print("加载入文件完成...")
4、load:把文件打开,并把字符串变换为数据类型
with open("../config/record.json",'r') as load_f: load_dict = json.load(load_f) print(load_dict) load_dict['smallberg'] = [8200,{1:[['Python',81],['shirt',300]]}] print(load_dict) with open("../config/record.json","w") as dump_f: json.dump(load_dict,dump_f)
5、遍历key value
test_json = {"a":1,"b":2,"c":3} for key,value in test_json.items(): print key,value
联系方式:emhhbmdfbGlhbmcxOTkxQDEyNi5jb20=
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-02-04 Andriod 从源码的角度详解View,ViewGroup的Touch事件的分发机制
2017-02-04 Android 报错Android - Performing stop of activity that is not resumed