linux Python 怎么将dict字典转换成json文件保存 - Python 怎么将dict字典转换成json文件保存
文章目录
Python 怎么将dict字典转换成json文件保存1. json.dump()2. with open("要打开的文件路径/保存的地址file_name", "w") as outfiles:
Python 怎么将dict字典转换成json文件保存
1. json.dump()
公式:json.dump(person_dict, json_file, indent=4)
person_dict: 要被转换的dict名json_file: 转换后要被保存到的文件名indent:表示缩进, =4表示key:value前缩进4个空格
2. with open(“要打开的文件路径/保存的地址file_name”, “w”) as outfiles:
w : 写一个json文件r:读一个json文件a: 追加内容
# 将dict这个字典转换为json格式,并保存在task1.json这个文件里。每组key:value前缩进4个空格 拼多多帮助力入口
with open("task1.json", "w") as outfiles:
json.dump(dict, outfiles, indent = 4)
相关链接