ls1 = {} ls2 = [] ls3 = [['班级', '学号', '姓名', '测试1(百分制)', '测试2(百分制)', '测试3(百分制)', '测试4(百分制)', '总分', '备注']] with open('total.csv', mode='r') as f: r = f.readlines() n = 0 for l in r: ls4 = l.split(',') ls4[-1] = ls4[-1][:-1] if n == 0: for t in ls4[3:]: num = '' for s in t: if s.isdigit(): num += s if s == '.': break ls2.append(int(num)) else: lens = len(ls4[3:]) for j in range(lens): if ls4[3 + j] == '缺考': ls4[3 + j] = 0 ls1[ls4[2]] = list(map(int, [ls4[3], ls4[4], ls4[5], ls4[6]])) n += 1 for t, v in ls1.items(): f = len(v) for k in range(f): v[k] = (v[k] / ls2[k]) * 100 // 1 p = sum(v) v.append(p) with open('total.csv', mode='r') as f: n = f.readlines() for l in r: ls = l.split(',') ls[-1] = ls[-1][:-1] ls3.append(ls[:3]) a = 1 for t, v in ls1.items(): for o in v: o = str(o) ls3[a + 1].append(o) a += 1 ls3.pop(1) with open('最终成绩单.csv', mode='w') as f: for i in ls3: s = ','.join(i) s += '\n' f.write(s) print(i) 改一下变量
时间: 2024-02-14 13:10:56 浏览: 117
好的,我来帮您改一下变量名:
```python
dict_scores = {}
list_max_scores = []
list_table_header = [['班级', '学号', '姓名', '测试1(百分制)', '测试2(百分制)', '测试3(百分制)', '测试4(百分制)', '总分', '备注']]
with open('total.csv', mode='r') as f:
r = f.readlines()
n = 0
for l in r:
list_line = l.split(',')
list_line[-1] = list_line[-1][:-1]
if n == 0:
for t in list_line[3:]:
num = ''
for s in t:
if s.isdigit():
num += s
if s == '.':
break
list_max_scores.append(int(num))
else:
len_scores = len(list_line[3:])
for j in range(len_scores):
if list_line[3 + j] == '缺考':
list_line[3 + j] = 0
dict_scores[list_line[2]] = list(map(int, [list_line[3], list_line[4], list_line[5], list_line[6]]))
n += 1
for t, v in dict_scores.items():
len_scores = len(v)
for k in range(len_scores):
v[k] = (v[k] / list_max_scores[k]) * 100 // 1
total_score = sum(v)
v.append(total_score)
with open('total.csv', mode='r') as f:
n = f.readlines()
for l in r:
list_line = l.split(',')
list_line[-1] = list_line[-1][:-1]
list_table_header.append(list_line[:3])
row_num = 1
for t, v in dict_scores.items():
for score in v:
score = str(score)
list_table_header[row_num + 1].append(score)
row_num += 1
list_table_header.pop(1)
with open('最终成绩单.csv', mode='w') as f:
for i in list_table_header:
line = ','.join(i)
line += '\n'
f.write(line)
print(i)
```
阅读全文