html tool

2022年10月27日星期四

转:python将字典保存成json 文件

 https://blog.csdn.net/leviopku/article/details/103773219

python将字典保存成json

import json

a = {

    "name": "dabao",

    "id":123,

    "hobby": {

        "sport": "basketball",

        "book": "python study"

    }

}

b = json.dumps(a)

f2 = open('new_json.json', 'w')

f2.write(b)

f2.close()

首先通过json.dumps()把dict降级为字符串。再将字符串写入json文件中。就是这么简单


没有评论:

发表评论