""" Auther:少校 Time:2025/4/10 16:22 越努力,越幸运 """ # 1. 如何做数据持久化 """ 1) 确定持久化的数据 2) 创建文件,设计文件的初始内容 3) 在程序中需要这个数据的时候,从文件中读取数据。当这个数据发生变化的时候必须将最新的数据更新到文件中 """ #案例:写程序打印当前程序启动的次数 s=open("./ceshi/count.txt","r",encoding="utf-8") count = int(s.read()) count += 1 s=open("./ceshi/count.txt","w",encoding="utf-8") s.write(str(count)) s.close() print(count) # 练习: 写程序实现功能,每次运行程序可以录入一个学生的名字,录入后打印已经录入过的所有学生的名字 name=input("请输入一个名字:") age=input("年龄:") gender=input("性别:") s=open("./ceshi/姓名.txt","r",encoding="utf-8") list1=(eval(s.read())) list1.append({"姓名":name,"年龄":age,"性别":gender}) s=open("./ceshi/姓名.txt","w",encoding="utf-8") s.write(str(list1)) print(list1) s.close()
24.数据持久化方法
本节691字2025-04-11 09:26:23