python 将数据字典dict 写入Json文件
需要调用json库
import json dictObj = { 'andy':{ 'age': 23, 'city': 'shanghai', 'skill': 'python' }, 'william': { 'age': 33, 'city': 'hangzhou', 'skill': 'js' } } jsObj = json.dumps(dictObj, indent=4) # indent参数是换行和缩进 fileObject = open('1.json', 'w') fileObject.write(jsObj) fileObject.close()
#最终写入的json文件格式:
{ "andy": { "age": 23, "city": "shanghai", "skill": "python" }, "william": { "age": 33, "city": "hangzhou", "skill": "js" } }