请编写函数,从文件中读取成绩不及格的学生信息,将结果写入另一个文件。
时间: 2024-05-10 08:20:19 浏览: 25
以下是Python的实现代码:
```python
def find_failing_students(input_file, output_file):
# 打开输入文件和输出文件
with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out:
# 遍历输入文件的每一行
for line in f_in:
# 从每一行中提取学生姓名和分数
name, score = line.strip().split()
# 判断分数是否小于60
if int(score) < 60:
# 如果分数小于60,则将学生信息写入输出文件
f_out.write(name + ' ' + score + '\n')
```
调用示例:
```python
find_failing_students('input.txt', 'output.txt')
```
相关问题
请用C语言编写函数,从文件中读取成绩不及格的学生信息,将结果写入另一个文件。
以下是一个可能的实现:
```c
#include <stdio.h>
typedef struct {
char name[20];
int score;
} Student;
void write_failed_students(const char* input_filename, const char* output_filename) {
FILE* input_file = fopen(input_filename, "r");
if (input_file == NULL) {
printf("Failed to open input file %s\n", input_filename);
return;
}
FILE* output_file = fopen(output_filename, "w");
if (output_file == NULL) {
fclose(input_file);
printf("Failed to open output file %s\n", output_filename);
return;
}
Student student;
while (fscanf(input_file, "%s %d", student.name, &student.score) != EOF) {
if (student.score < 60) {
fprintf(output_file, "%s %d\n", student.name, student.score);
}
}
fclose(input_file);
fclose(output_file);
}
int main() {
write_failed_students("input.txt", "output.txt");
return 0;
}
```
这个程序定义了一个 `Student` 结构体表示学生,包含一个名字和一个分数。`write_failed_students` 函数接受两个文件名参数,打开输入文件并逐行读取学生信息,如果该学生的分数不及格(小于60),则将该学生的信息写入输出文件。最后,两个文件都会被关闭。在 `main` 函数中,我们调用 `write_failed_students` 来执行这个操作。
python读取txt成绩信息并写入结果文件
假设txt文件中每行为一个学生的成绩,格式为“姓名 成绩”,如下所示:
```
张三 90
李四 85
王五 92
```
可以使用Python中的文件操作函数来读取txt文件中的成绩信息,然后将结果写入到另一个文件中。代码如下:
```python
# 打开txt文件
with open('scores.txt', 'r') as f:
lines = f.readlines()
# 处理每行数据
results = []
for line in lines:
name, score = line.strip().split(' ')
score = int(score)
if score >= 90:
level = '优秀'
elif score >= 80:
level = '良好'
elif score >= 60:
level = '及格'
else:
level = '不及格'
result = f'{name}的成绩为{score}分,评级为{level}'
results.append(result)
# 将结果写入文件
with open('result.txt', 'w') as f:
f.write('\n'.join(results))
```
上述代码中,首先使用`with open('scores.txt', 'r') as f`打开`scores.txt`文件,并使用`f.readlines()`读取每一行数据,然后使用`strip()`和`split()`函数处理每行数据,将姓名和成绩分别提取出来。
接着,使用if语句根据成绩评级,并将结果存储到一个列表中。最后,使用`with open('result.txt', 'w') as f`打开`result.txt`文件,并使用`f.write('\n'.join(results))`将结果写入该文件。
执行上述代码后,会在当前目录下生成`result.txt`文件,并将每个学生的成绩和评级写入该文件中。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)