Day_13 · 文件读写与 JSON:让数据活到下次运行 #14
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
今日知识点
with open("note.txt", "w") as f: f.write(内容)(w= 覆盖写)with open("note.txt", "r") as f: 内容 = f.read()with会自动关文件,不用手动 closejson.dumps变文本、json.loads变回数据先学(教程)
任务(后做)
新建
day13.py:note.txt,且内容一致。(注意encoding="utf-8",否则中文会乱码。)json.dump存进users.json,再读回来遍历打印每个人的名字。
完成标准
json.dump和json.load各是干嘛的提交方式:把
day13.py代码贴在当日工单评论里。参考解答(先做再看)
1
day13完成
1
。
import json user={"name":"张三","age":18,"city":"青岛"} text=input("写一句话存起来:") with open("note.txt","w",encoding="utf-8") as f: f.write(text) with open("note.txt","r",encoding="utf-8") as f: content=f.read() print("读回来了",content) with open("user.json","w",encoding="utf-8") as f: json.dump(user,f,ensure_ascii=False) with open("user.json","r",encoding="utf-8") as f: loaded=json.load(f) print(loaded["name"],loaded["age"])13
。
Day_13 · 文件读写与 JSON
写/读 note.txt,json.dump/load 保存用户数据并修改后再读回