python将本班级的学生信息保存在 SY10_1.txt中, 每行中信息依次为: StuNo 、StuName 、StuAge、StuPhone、Class、Dept,逗号隔开
时间: 2024-06-09 16:11:31 浏览: 8
好的,你可以通过以下代码来实现将学生信息保存在 SY10_1.txt 中:
```python
# 学生信息
students = [
{'StuNo': '001', 'StuName': '张三', 'StuAge': '18', 'StuPhone': '13812345678', 'Class': 'SY10', 'Dept': '计算机科学与技术'},
{'StuNo': '002', 'StuName': '李四', 'StuAge': '19', 'StuPhone': '13987654321', 'Class': 'SY10', 'Dept': '计算机科学与技术'},
{'StuNo': '003', 'StuName': '王五', 'StuAge': '20', 'StuPhone': '13611112222', 'Class': 'SY10', 'Dept': '计算机科学与技术'}
]
# 将学生信息保存到文件中
with open('SY10_1.txt', 'w') as f:
for student in students:
line = ','.join(student.values()) + '\n'
f.write(line)
```
以上代码中,我们首先定义了一个 `students` 列表,其中每个元素都是一个字典,表示一个学生的信息。然后,我们打开文件 `SY10_1.txt`,使用循环将每个学生的信息写入文件中。在每一行中,我们使用逗号将不同的信息分隔开,并在最后加上一个换行符,以便在读取文件时,每行信息可以单独读取。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![dll](https://img-home.csdnimg.cn/images/20210720083646.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![m](https://img-home.csdnimg.cn/images/20210720083646.png)