Python-用字母等级替换CSV文件中的整数

时间:2018-11-07 16:04:44

标签: python

所以我在BSU参加CS班,这里的任务是读取CSV文件,其中包含班级名称,这些班级的分数以及该班级的学时数。我们被告知要用适当的字母等级代替班级的成绩,但我一直在努力寻找方法?如果有人知道如何轻松地将某些内容集成到其中,那么我可以用字母等级替换csv文件中的整数,而不是heklp。

import csv

courses = []
with open("courses.csv") as file:
    reader = csv.reader(file, delimiter=",")
    for row in reader:
        print(row)

def read_csv(file):
    courses = []
    with open(file) as file:
        reader = csv.reader(file, delimiter='.')
        next(reader)
        for row in reader:
            courses.append(row)
    return courses

def print_scores():
    i=0
    file = "courses.csv"
    courses = read_csv(file)
    print("{0:10} {1:10} {2:10}".format("Course", "Score", "Cerdits"))
    for row in file:
        score = row[i][1]
        row[i][1] = grade(score)
        i += 1


def grade(score):
    if score > 100:
        grade = "Doesn't matter, ur a nurd"
    elif score >= 90:
        grade = "A"
    elif score >= 80:
        grade = "B"
    elif score >= 70:
        grade = "C"
    elif score >= 60:
        grade = "D"
    else:
        grade = "F"
    return grade

0 个答案:

没有答案