第一步,按照姓名,年龄,性别生成三个的字典,把他们用open()写入到内存中;第二步,设计一个修改模块,修改他们的姓名 年龄 性别 并替换到内存中
第一步:生成三个字典并写入到内存中
```python
import json
name_dict = {"张三": 25, "李四": 30, "王五": 28}
age_dict = {"张三": 25, "李四": 30, "王五": 28}
gender_dict = {"张三": "男", "李四": "女", "王五": "男"}
with open("data.json", "w", encoding="utf-8") as f:
json.dump({"name": name_dict, "age": age_dict, "gender": gender_dict}, f, ensure_ascii=False)
```
第二步:设计一个修改模块,修改他们的姓名、年龄、性别并替换到内存中
```python
def update_info(name, age, gender):
with open("data.json", "r", encoding="utf-8") as f:
data = json.load(f)
data["name"][name] = age
data["age"][name] = age
data["gender"][name] = gender
with open("data.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False)
# 示例:修改张三的信息
update_info("张三", 26, "男")
```
```python
import json
name_dict = {"张三": 25, "李四": 30, "王五": 28}
age_dict = {"张三": 25, "李四": 30, "王五": 28}
gender_dict = {"张三": "男", "李四": "女", "王五": "男"}
with open("data.json", "w", encoding="utf-8") as f:
json.dump({"name": name_dict, "age": age_dict, "gender": gender_dict}, f, ensure_ascii=False)
```
第二步:设计一个修改模块,修改他们的姓名、年龄、性别并替换到内存中
```python
def update_info(name, age, gender):
with open("data.json", "r", encoding="utf-8") as f:
data = json.load(f)
data["name"][name] = age
data["age"][name] = age
data["gender"][name] = gender
with open("data.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False)
# 示例:修改张三的信息
update_info("张三", 26, "男")
```