python 如何将字典写为json文件
作者:紫陌幽茗 时间:2021-07-22 18:39:39
python 将字典写为json文件
字典结构如下
res = {
"data":[]
}
temp = {
"name":name,
"cls":cls
}
res["data"].append(temp)
写为json
具体代码如下:
json_data = json.dumps(res)
with open('E:/res.json', 'a') as f_six:
f_six.write(json_data)
即可完成需求~~
Python txt文件读取写入字典(json、eval)
使用json转换方法
1、字典写入txt
import json
dic = {
'andy':{
'age': 23,
'city': 'beijing',
'skill': 'python'
},
'william': {
'age': 25,
'city': 'shanghai',
'skill': 'js'
}
}
js = json.dumps(dic)
file = open('test.txt', 'w')
file.write(js)
file.close()
2、读取txt中的字典
import json
file = open('test.txt', 'r')
js = file.read()
dic = json.loads(js)
print(dic)
file.close()
使用str转换方法
1、字典写入txt
dic = {
'andy':{
'age': 23,
'city': 'beijing',
'skill': 'python'
},
'william': {
'age': 25,
'city': 'shanghai',
'skill': 'js'
}
}
fw = open("test.txt",'w+')
fw.write(str(dic)) #把字典转化为str
fw.close()
2、读取txt中字典
fr = open("test.txt",'r+')
dic = eval(fr.read()) #读取的str转换为字典
print(dic)
fr.close()
来源:https://blog.csdn.net/weixin_43165512/article/details/108704653
标签:python,字典,json文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
从SNS看社会化界面设计(一)
2009-02-23 12:21:00
![](https://img.aspxhome.com/file/UploadPic/20092/23/1-73s.jpg)
CentOS7下安装python3.6.8的教程详解
2022-02-17 17:27:29
![](https://img.aspxhome.com/file/2023/9/79399_0s.png)
浅谈Python3实现两个矩形的交并比(IoU)
2021-09-06 19:31:56
![](https://img.aspxhome.com/file/2023/7/67247_0s.jpg)
详解Python自动化之文件自动化处理
2022-07-24 03:50:02
Python实现从PPT中导出高分辨率图片
2023-01-03 16:29:08
![](https://img.aspxhome.com/file/2023/3/134913_0s.png)
sqlserver 快速生成汉字的首拼字母的函数(经典)
2012-06-06 20:16:41
微信跳一跳游戏python脚本
2022-03-31 02:24:27
![](https://img.aspxhome.com/file/2023/6/66806_0s.jpg)
Python绘制简单散点图的方法
2023-02-22 02:01:07
![](https://img.aspxhome.com/file/2023/8/87768_0s.jpg)
基于Python实现围棋游戏的示例代码
2022-08-21 07:56:39
![](https://img.aspxhome.com/file/2023/2/92072_0s.gif)
如何使用Python逆向抓取APP数据
2022-03-12 11:03:41
![](https://img.aspxhome.com/file/2023/0/121600_0s.png)
Python中处理unchecked未捕获异常实例
2022-08-03 23:49:54
Python求两点之间的直线距离(2种实现方法)
2021-03-28 19:24:46
Django 如何实现文件上传下载
2021-07-16 02:54:15
![](https://img.aspxhome.com/file/2023/0/86270_0s.png)
浅谈ROC曲线的最佳阈值如何选取
2021-10-07 07:49:29
![](https://img.aspxhome.com/file/2023/9/103009_0s.jpg)
Pytorch中的gather使用方法
2021-11-22 06:11:49
Python深入06——python的内存管理详解
2021-07-29 05:18:26
![](https://img.aspxhome.com/file/2023/2/87682_0s.jpg)
用 Django 开发一个 Python Web API的方法步骤
2021-05-24 02:36:11
openCV实现图像融合的示例代码
2022-05-20 03:28:16
![](https://img.aspxhome.com/file/2023/4/110574_0s.jpg)
Python实现运行其他程序的四种方式实例分析
2023-08-03 00:48:24
![](https://img.aspxhome.com/file/2023/1/61741_0s.png)
Python开源自动化工具Playwright安装及介绍使用
2023-08-20 13:17:43
![](https://img.aspxhome.com/file/2023/5/115165_0s.png)