Python Json模块中dumps、loads、dump、load函数介绍
2021/4/18 22:25:39
本文主要是介绍Python Json模块中dumps、loads、dump、load函数介绍,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
https://www.jb51.net/article/140151.htm
import json name_emb = {'a': '1111', 'b': '2222', 'c': '3333', 'd': '4444'} jsDumps = json.dumps(name_emb) jsLoads = json.loads(jsDumps) print(name_emb) print(jsDumps) print(jsLoads) print(type(name_emb)) print(type(jsDumps)) print(type(jsLoads)) print("=================================================") emb_filename = ('emb_json.json') # solution 1 jsObj = json.dumps(name_emb) with open(emb_filename, "w") as f: f.write(jsObj) f.close() # solution 2 json.dump(name_emb, open(emb_filename, "w")) jsObj = json.load(open(emb_filename)) print(jsObj) print(type(jsObj)) for key in jsObj.keys(): print('key: %s value: %s' % (key, jsObj.get(key)))
C:\Users\huzhenyu\AppData\Local\Programs\Python\Python35\python.exe F:/PythonData/students/p/sns.py
{'a': '1111', 'b': '2222', 'c': '3333', 'd': '4444'}
{"a": "1111", "b": "2222", "c": "3333", "d": "4444"}
{'a': '1111', 'b': '2222', 'c': '3333', 'd': '4444'}
<class 'dict'>
<class 'str'>
<class 'dict'>
=================================================
{'a': '1111', 'b': '2222', 'c': '3333', 'd': '4444'}
<class 'dict'>
key: a value: 1111
key: b value: 2222
key: c value: 3333
key: d value: 4444
Process finished with exit code 0
这篇关于Python Json模块中dumps、loads、dump、load函数介绍的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-28Vue入门教程:从零开始搭建第一个Vue项目
- 2024-12-28Vue CLI入门指南:快速搭建Vue项目
- 2024-12-28Vue3基础知识入门教程
- 2024-12-28Vue3公共组件开发与使用入门教程
- 2024-12-28Vue CLI学习:新手入门教程
- 2024-12-28Vue CLI学习:轻松入门与实践指南
- 2024-12-28Vue3公共组件学习入门指南
- 2024-12-28Vue3公共组件学习:从入门到上手实战
- 2024-12-28Vue3学习:从入门到初级实战教程
- 2024-12-28Vue3学习:新手入门与初级教程