深度阅读

json保存配置文件

作者
作者
2023年08月22日
更新时间
4.28 分钟
阅读时间
0
阅读量

json保存配置文件


import json

def save(data):

with open('config.json', 'w') as fw:
<h1>将字典转化为字符串</h1>
<h1>json_str = json.dumps(data)</h1>
<h1>fw.write(json_str)</h1>
<h1>上面两句等同于下面这句</h1>
json.dump(data,fw)
<h1>load json data from file</h1>
def load():

try:

with open('config.json','r') as f:

data = json.load(f)

return data

except:

with open('config.json', 'w') as fw:

json.dump({},fw)

return {}

CONF=load()


相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。