编写程序,用zip方式创建学生信息字典,包含学生姓名,学号,性别,身高
时间: 2024-05-05 13:17:57 浏览: 11
,体重等信息。你可以使用Python编写这个程序,以下是示例代码:
```
import zipfile
# 创建学生信息字典
student_dict = {
'001': {'name': '张三', 'gender': '男', 'height': 175, 'weight': 65},
'002': {'name': '李四', 'gender': '女', 'height': 160, 'weight': 50},
'003': {'name': '王五', 'gender': '男', 'height': 180, 'weight': 75}
}
# 将字典导出为文本文件
with open('students.txt', 'w') as f:
for std_id, std_info in student_dict.items():
f.write(f'{std_id},{std_info["name"]},{std_info["gender"]},{std_info["height"]},{std_info["weight"]}\n')
# 将文本文件压缩为 zip 文件
with zipfile.ZipFile('students.zip', 'w') as z:
z.write('students.txt')
```
以上代码首先定义了一个学生信息字典,包括三个学生的学号、姓名、性别、身高和体重等基本信息。然后将这个字典导出为文本文件,每行一个学生信息,使用逗号分隔不同字段。最后使用 Python 自带的 zipfile 模块将文本文件压缩为 zip 文件。