linux文件方式录入信息代码
时间: 2024-02-24 16:56:15 浏览: 24
好的,以下是使用文件方式录入学生信息和课程信息的示例代码:
1. student.txt文件中记录学生信息,每行格式为:学号 姓名 年龄 性别
```
001 张三 18 男
002 李四 19 女
003 王五 20 男
```
2. class.txt文件中记录课程信息,每行格式为:课程编号 课程名称 授课教师
```
001 计算机基础 刘老师
002 数据结构 张老师
003 操作系统 王老师
```
使用Python代码读取和写入文件可以轻松实现学生信息和课程信息的录入和修改。以下是示例代码:
```python
# 读取student.txt文件
with open('student.txt', 'r') as f:
student_lines = f.readlines()
# 读取class.txt文件
with open('class.txt', 'r') as f:
class_lines = f.readlines()
# 输出学生信息和课程信息
print('学生信息:')
for line in student_lines:
stu_info = line.strip().split()
print('学号:{},姓名:{},年龄:{},性别:{}'.format(stu_info[0], stu_info[1], stu_info[2], stu_info[3]))
print('课程信息:')
for line in class_lines:
class_info = line.strip().split()
print('课程编号:{},课程名称:{},授课教师:{}'.format(class_info[0], class_info[1], class_info[2]))
# 写入新的学生信息和课程信息
with open('student.txt', 'a') as f:
f.write('004 赵六 21 男\n')
with open('class.txt', 'a') as f:
f.write('004 计算机网络 赵老师\n')
```
这里使用了Python的文件操作函数,先读取文件内容,然后输出学生信息和课程信息,最后写入新的学生信息和课程信息。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)
![](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)
![](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)
![](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)