文件score txt保存了学生的干时成绩和期末成绩,编气程序读取所有成绩,计算总评成绩(四舍五 入到整数) ,其中总评成绩一子时成绩*40%+期末成绩+60%。最后按总评成绩降序排列保仔至一个新文件中。python
时间: 2024-02-16 07:04:58 浏览: 22
好的,我可以为您实现这个功能。以下是Python代码:
```python
# 读取所有成绩
with open('score.txt', 'r') as f:
lines = f.readlines()
# 计算总评成绩并保存到列表中
scores = []
for line in lines:
grade = line.strip().split()
total_score = round(float(grade[0]) * 0.4 + float(grade[1]) * 0.6)
scores.append((total_score, line.strip()))
# 按照总评成绩降序排列
scores = sorted(scores, reverse=True)
# 将排好序的成绩写入新文件中
with open('sorted_score.txt', 'w') as f:
for score in scores:
f.write(score[1] + '\n')
```
请将上述代码保存为一个名为 `sort_score.py` 的文件,然后将需要排序的成绩文件命名为 `score.txt`。将这两个文件放在同一目录下,然后运行 `sort_score.py` 文件,即可在当前目录下生成一个名为 `sorted_score.txt` 的新文件,其中包含按照总评成绩降序排列的所有成绩。
相关问题
文件score.txt保存了学生的平时成绩和期末成绩,内容如下: 学号,平时成绩,期末成绩 9999180101,77,88 9999180102,91,85 9999180103,87,96 9999180104,70,68 9999180105,86,72 编写程序读取所有成绩,计算总评成绩(四舍五入到整数),其中总评成绩=平时成绩40%+期末成绩60%。最后按总评成绩降序排列后保存至一个新的文件内,文件内容应该如下:
首先,我们需要读取文件并计算每个学生的总评成绩。代码如下:
```python
with open('score.txt', 'r') as f:
lines = f.readlines()
scores = []
for line in lines:
stu_info = line.strip().split(',')
stu_id = stu_info[0]
daily_score = int(stu_info[1])
final_score = int(stu_info[2])
total_score = round(daily_score * 0.4 + final_score * 0.6)
scores.append((stu_id, total_score))
```
上述代码中,我们使用了`with open()`语句来打开文件并读取每一行。然后,我们使用`strip()`方法去除每行字符串的空格和换行符,并使用`split()`方法将每行字符串按照逗号分隔为学号、平时成绩和期末成绩。接着,我们将平时成绩和期末成绩转换为整型,并计算每个学生的总评成绩。最后,我们将每个学生的学号和总评成绩保存到一个元组中,并将所有元组保存到一个列表`scores`中。
接下来,我们需要按照总评成绩降序排列,并将结果保存到新的文件内。代码如下:
```python
scores_sorted = sorted(scores, key=lambda x: x[1], reverse=True)
with open('result.txt', 'w') as f:
for score in scores_sorted:
f.write(score[0] + ',' + str(score[1]) + '\n')
```
上述代码中,我们使用`sorted()`函数对`scores`列表进行排序,排序规则是按照元素的第二个值即总评成绩进行降序排列。排序后的结果保存在`scores_sorted`列表中。接着,我们使用`with open()`语句打开新文件`result.txt`并将排序后的结果写入到文件中,每个元素占一行。在写入时,我们使用逗号将学号和总评成绩连接起来,并在最后加上一个换行符`\n`。
完整代码如下:
```python
with open('score.txt', 'r') as f:
lines = f.readlines()
scores = []
for line in lines:
stu_info = line.strip().split(',')
stu_id = stu_info[0]
daily_score = int(stu_info[1])
final_score = int(stu_info[2])
total_score = round(daily_score * 0.4 + final_score * 0.6)
scores.append((stu_id, total_score))
scores_sorted = sorted(scores, key=lambda x: x[1], reverse=True)
with open('result.txt', 'w') as f:
for score in scores_sorted:
f.write(score[0] + ',' + str(score[1]) + '\n')
```
执行完上述代码后,会在当前目录下生成一个新的文件`result.txt`,其中包含按总评成绩降序排列后的学生信息。
14.文件score.txt保存了学生的平时成绩和期末成绩,内容如下:\n学号,平时成绩,期末成绩\n9999180101,77,88\n9999180102,91,85\n9999180103,87,96\n9999180104,70,68\n9999180105,86,72\n编写程序读取所有成绩,计算总评成绩(四舍五入到整数),其中总评成绩=平时成绩40%+期末成绩60%。最后按总评成绩降序排列后保存至一个新的文件内,文件内容应该如下:
学号 总评成绩
9999180103 91
9999180102 89
9999180105 78
9999180101 76
9999180104 69
以下是Python代码实现:
```python
import csv
# 读取文件
with open('score.txt', 'r') as f:
reader = csv.reader(f)
next(reader) # 跳过第一行标题
scores = []
for row in reader:
stu_id = row[0]
usual_score = int(row[1])
final_score = int(row[2])
total_score = round(usual_score * 0.4 + final_score * 0.6) # 计算总评成绩并四舍五入到整数
scores.append((stu_id, total_score))
# 按总评成绩降序排列
scores_sorted = sorted(scores, key=lambda x: x[1], reverse=True)
# 保存至新文件
with open('total_score.txt', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['学号', '总评成绩'])
for row in scores_sorted:
writer.writerow([row[0], row[1]])
```
注意事项:
- 使用Python的csv模块来读取和保存文件,这样可以方便地处理逗号分隔的数据。
- 在读取文件时,使用next()函数跳过第一行标题。
- 在计算总评成绩时,使用round()函数将小数四舍五入到整数。
- 在保存结果到新文件时,使用newline=''参数来避免生成空行。