Javascript

Python Json模块中dumps、loads、dump、load函数介绍

本文主要是介绍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函数介绍的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!