#-*-coding:utf-8-*-
import json
informations = {
'姓': '张',
'名': '三',
'性别': '男',
'年龄': 18,
'成绩': {
'语文': 86,
'数学': 66
}
}
path = '成绩单.json'
json_str = json.dumps(informations, ensure_ascii=False, indent=4) # 缩进4字符
with open(path, 'w') as json_file:
json_file.write(json_str)
使用 notepad++ 打开成绩单.json文件,可以看到如下图:

本文介绍了一个简单的Python脚本,用于将学生信息和成绩以JSON格式存储到文件中。通过使用json模块,我们可以轻松地将Python字典转换为JSON字符串,并将其写入文件。这种方法适用于小型数据集的存储和读取。
3799

被折叠的 条评论
为什么被折叠?



