Day_13 · 文件读写与 JSON:让数据活到下次运行 #14

Open
opened 2026-08-22 17:26:37 +08:00 by shimmer · 28 comments
Owner

题目与教程详见仓库对应目录 Day_13/README.md(本仓库已克隆即得)。

预计耗时:25~30 分钟
程序一关,变量就没了。想让数据存下来(这就是"持久化",数据库干的事),先从文件开始。
Django 的数据库、缓存的本质,都是"把数据存起来再读回来"。

今日知识点

  • 写文件:with open("note.txt", "w") as f: f.write(内容)(w = 覆盖写)
  • 读文件:with open("note.txt", "r") as f: 内容 = f.read()
  • with 会自动关文件,不用手动 close
  • JSON:一种"把字典/列表变成文本"的格式,json.dumps 变文本、json.loads 变回数据
  • 为什么重要:HTTP 接口传输的数据、配置文件,全是 JSON 格式

先学(教程)

任务(后做)

新建 day13.py:

  1. 写入再读回:
    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)
    
    运行后确认同目录出现了 note.txt,且内容一致。(注意 encoding="utf-8",否则中文会乱码。)
  2. 用 JSON 存字典:
    import json
    
    user = {"name": "张三", "age": 18, "city": "青岛"}
    
    with open("user.json", "w", encoding="utf-8") as f:
        json.dump(user, f, ensure_ascii=False)   # ensure_ascii=False 让中文可读
    
    with open("user.json", "r", encoding="utf-8") as f:
        loaded = json.load(f)
    
    print(loaded["name"], loaded["age"])         # 从文件里还原的字典
    
  3. 进阶(加分):把 Day_5 的"三个用户列表"用 json.dump 存进 users.json,
    再读回来遍历打印每个人的名字。

完成标准

  • note.txt 和 user.json 出现在同目录,读回内容一致
  • 能说出 json.dump 和 json.load 各是干嘛的
  • (进阶)用户列表存取成功

提交方式:把 day13.py 代码贴在当日工单评论里。

参考解答(先做再看)

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:
    print("读回来的是:", f.read())

import json
user = {"name": "张三", "age": 18, "city": "青岛"}
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"])

users = [
    {"name": "张三", "age": 19},
    {"name": "李四", "age": 20},
    {"name": "王五", "age": 18},
]
with open("users.json", "w", encoding="utf-8") as f:
    json.dump(users, f, ensure_ascii=False)
with open("users.json", "r", encoding="utf-8") as f:
    for u in json.load(f):
        print(u["name"])
> 题目与教程详见仓库对应目录 Day_13/README.md(本仓库已克隆即得)。 > 预计耗时:25~30 分钟 > 程序一关,变量就没了。想让数据存下来(这就是"持久化",数据库干的事),先从文件开始。 > Django 的数据库、缓存的本质,都是"把数据存起来再读回来"。 ## 今日知识点 - 写文件:`with open("note.txt", "w") as f: f.write(内容)`(`w` = 覆盖写) - 读文件:`with open("note.txt", "r") as f: 内容 = f.read()` - `with` 会自动关文件,不用手动 close - **JSON**:一种"把字典/列表变成文本"的格式,`json.dumps` 变文本、`json.loads` 变回数据 - 为什么重要:HTTP 接口传输的数据、配置文件,全是 JSON 格式 ## 先学(教程) - 菜鸟教程("文件方法"一节,看 open/read/write 即可):https://www.runoob.com/python3/python3-file-methods.html - 菜鸟教程("JSON"一节):https://www.runoob.com/python3/python3-json.html - B站黑马教程(看讲"文件操作"的几集):https://www.bilibili.com/video/BV1qW4y1a7fU/ ## 任务(后做) 新建 `day13.py`: 1. 写入再读回: ```python 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) ``` 运行后确认同目录出现了 `note.txt`,且内容一致。(注意 `encoding="utf-8"`,否则中文会乱码。) 2. 用 JSON 存字典: ```python import json user = {"name": "张三", "age": 18, "city": "青岛"} with open("user.json", "w", encoding="utf-8") as f: json.dump(user, f, ensure_ascii=False) # ensure_ascii=False 让中文可读 with open("user.json", "r", encoding="utf-8") as f: loaded = json.load(f) print(loaded["name"], loaded["age"]) # 从文件里还原的字典 ``` 3. **进阶(加分)**:把 Day_5 的"三个用户列表"用 `json.dump` 存进 `users.json`, 再读回来遍历打印每个人的名字。 ## 完成标准 - [ ] note.txt 和 user.json 出现在同目录,读回内容一致 - [ ] 能说出 `json.dump` 和 `json.load` 各是干嘛的 - [ ] (进阶)用户列表存取成功 **提交方式**:把 `day13.py` 代码贴在当日工单评论里。 ## 参考解答(先做再看) ```python 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: print("读回来的是:", f.read()) import json user = {"name": "张三", "age": 18, "city": "青岛"} 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"]) users = [ {"name": "张三", "age": 19}, {"name": "李四", "age": 20}, {"name": "王五", "age": 18}, ] with open("users.json", "w", encoding="utf-8") as f: json.dump(users, f, ensure_ascii=False) with open("users.json", "r", encoding="utf-8") as f: for u in json.load(f): print(u["name"]) ```

image.png

![image.png](/attachments/5064e912-b787-44a3-8ca2-1e835884ec86)

image.png

![image.png](/attachments/c759132f-a561-4ed5-b432-e7d066665904)
283 KiB
image.png
<img width="1382" alt="image.png" src="attachments/a4c1248d-c917-441a-ac8c-9a256a2796d6">
163 KiB
image.png
<img width="533" alt="image.png" src="attachments/31551d37-77fb-49cb-83ec-cf7a47654979">
152 KiB
image.png
<img width="506" alt="image.png" src="attachments/6c243abf-4bc6-422f-8ec2-b2f2038de3b3">
156 KiB
image.png
<img width="477" alt="image.png" src="attachments/102e0e62-52ff-41e8-9c8b-18935b0280eb">
105 KiB
屏幕截图 2026-09-20 000831.png
<img width="1437" alt="屏幕截图 2026-09-20 000831.png" src="attachments/fbdfd169-4e5b-4fb3-806a-c027f1cc8918">
{8516EDC6-CE52-4D21-AB4C-5152CE37160E}.png
<img width="540" alt="{8516EDC6-CE52-4D21-AB4C-5152CE37160E}.png" src="attachments/c0361152-a636-4aeb-819c-71d62913677f">

image.png

import json

# 文件基本读写部分
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)

# json 存字典部分
user = {"name": "张三", "age": 18, "city": "青岛"}

with open("user.json", "w", encoding="utf-8") as f:
    json.dump(user, f, ensure_ascii=False)
    # ensure_ascii=False 让中文不被转义成 \uXXXX 形式

with open("user.json", "r", encoding="utf-8") as f:
    loaded = json.load(f)

print(loaded["name"], loaded["age"])

# 进阶部分
users = [
    {"name": "张三", "age": 19},
    {"name": "李四", "age": 20},
    {"name": "王五", "age": 18},
]

with open("users.json", "w", encoding="utf-8") as f:
    json.dump(users, f, ensure_ascii=False)

with open("users.json", "r", encoding="utf-8") as f:
    for u in json.load(f):
        print(u["name"])
![image.png](/attachments/8c347cd5-acb0-4f14-a12c-996420d0f534) ```python import json # 文件基本读写部分 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) # json 存字典部分 user = {"name": "张三", "age": 18, "city": "青岛"} with open("user.json", "w", encoding="utf-8") as f: json.dump(user, f, ensure_ascii=False) # ensure_ascii=False 让中文不被转义成 \uXXXX 形式 with open("user.json", "r", encoding="utf-8") as f: loaded = json.load(f) print(loaded["name"], loaded["age"]) # 进阶部分 users = [ {"name": "张三", "age": 19}, {"name": "李四", "age": 20}, {"name": "王五", "age": 18}, ] with open("users.json", "w", encoding="utf-8") as f: json.dump(users, f, ensure_ascii=False) with open("users.json", "r", encoding="utf-8") as f: for u in json.load(f): print(u["name"]) ```
272 KiB
屏幕截图 2026-09-22 135330.png
<img width="1279" alt="屏幕截图 2026-09-22 135330.png" src="attachments/273cc035-24e8-4478-8a86-8d08717964a0">

image.png

![image.png](/attachments/22cefd8a-ddf0-41c1-9b59-e3c6d4d09c70)
298 KiB

QQ_1790250536442.png

![QQ_1790250536442.png](/attachments/713b2581-bdb3-4c9b-98ec-474dfb3ab4a4)
image.png
<img width="1593" alt="image.png" src="attachments/a01e89b3-1e83-468f-b7ed-5d0a64500f36">
314 KiB
image.png
<img width="1436" alt="image.png" src="attachments/ec06c93a-3775-46ff-9464-4cdc68155835">
426 KiB
image.png
<img width="586" alt="image.png" src="attachments/c70e6154-3dfb-4d03-9934-b15585d8b72f">
238 KiB
image.png
<img width="1152" alt="image.png" src="attachments/2b97e4c5-6c52-4bf9-9189-aaab023a73ca">
248 KiB

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"])

`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"])`

image.png

![image.png](/attachments/af3e903d-bc36-4db5-829d-db09e1e9ce47)
369 KiB

image.png

![image.png](/attachments/59bcf7c9-32fd-4b46-a2b3-95731ab20ca4)
365 KiB
image.png
<img width="1280" alt="image.png" src="attachments/79e7cee9-b13b-4bfd-8ecf-71ddfb932f99">
318 KiB

Day_13 · 文件读写与 JSON

写/读 note.txt,json.dump/load 保存用户数据并修改后再读回

day13.png

**Day_13 · 文件读写与 JSON** 写/读 note.txt,json.dump/load 保存用户数据并修改后再读回 ![day13.png](https://cdn.jsdelivr.net/gh/xu675693517-hue/hello-daily-challenge@main/screenshots/day13.png)

image.png

![image.png](/attachments/92bc333f-ec69-4d15-86f9-31a08eee43fe)
Sign in to join this conversation.