编程语言
首页 > 编程语言> > 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

标签:load,jsObj,name,dump,emb,Python,json,4444,print
来源: https://www.cnblogs.com/zhenyu1/p/14674669.html