python 从csv文件计算GPA

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 从csv文件计算GPA相关的知识,希望对你有一定的参考价值。

# coding=utf8
#!/usr/bin/python
import csv
import sys

PATHS = ('/Users/Gimo/Desktop/1.csv',
         '/Users/Gimo/Desktop/2.csv',
         '/Users/Gimo/Desktop/3.csv',
         '/Users/Gimo/Desktop/4.csv',
         '/Users/Gimo/Desktop/5.csv'
         )

LEVEL = {
    '优秀': 95,
    '良好': 85,
    '中等': 75,
    '及格': 65,
    '不及格': 50
}


def get_point(grade):
    if grade in LEVEL:
        return (LEVEL[grade] - 50) / 10.0
    if '/' in grade:
        grade = grade.split('/')[-1]
        if 60 <= float(grade) <= 70:
            return (float(grade) - 50) / 10.0
        elif float(grade) > 70:
            return 2.0
        else:
            return 0
    if any([grade == '', grade == '旷考', grade == '取消']):
        return 0
    if float(grade) >= 60:
        return (float(grade) - 50) / 10.0
    return 0


def get_GPA(info_list, sum_credit):
    sum_credit = sum_credit
    sum_credit_point = 0
    for x in info_list:
        sum_credit_point += x[0] * x[1]
    GPA = sum_credit_point / sum_credit
    return GPA


def get_cvs_list(path):
    with open(path, 'rb') as csvfile:
        reader = csv.reader(csvfile)
        return list(reader)


def get_credit_list(path):
    credit_list = []
    for x in get_cvs_list(path)[2][4:]:
        credit = float(x.split('/')[-1])
        credit_list.append(credit)
    return credit_list


def get_person_info_by_name(path, name):
    for x in get_cvs_list(path)[2:]:
        if name in x:
            return x


def get_name_list(path):
    name_list = []
    for x in get_cvs_list(path)[3:]:
        name_list.append(x[2])
    return name_list


def get_point_list(person_info_list):
    point_list = []
    for x in person_info_list[4:]:
        point_list.append(get_point(x))
    return point_list


def get_sum_credit(person_info_list):
    return float(person_info_list[3])


def get_all_GPA():
    name_list = get_name_list(PATHS[0])
    for name in name_list:
        print name
        for term, path in enumerate(PATHS, 1):
            person_info_list = get_person_info_by_name(path, name)
            info_list = zip(
                get_point_list(person_info_list), get_credit_list(path))
            GPA = get_GPA(info_list, get_sum_credit(person_info_list))
            print term, round(GPA, 2)


def get_GPA_by_name(name):
    print name
    for term, path in enumerate(PATHS, 1):
            person_info_list = get_person_info_by_name(path, name)
            info_list = zip(
                get_point_list(person_info_list), get_credit_list(path))
            GPA = get_GPA(info_list, get_sum_credit(person_info_list))
            print term, round(GPA, 2)


def main():
    if len(sys.argv) == 1:
        name = raw_input('enter your name:')
        get_GPA_by_name(name)
    if len(sys.argv) == 2:
        if sys.argv[1] == 'all':
            get_all_GPA()
        else:
            name = sys.argv[1]
            get_GPA_by_name(name)

if __name__ == '__main__':
    main()

以上是关于python 从csv文件计算GPA的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 中使用函数计算 GPA

python UTMATE GPA计算用スクリプト

教你如何使用GPA导出模型,另送一个 GPA CSV2MESH Tool in unity

求python通过键盘输入成绩,用分支结构算成绩相应的绩点并输出成绩绩点,计算绩点公式=成绩-60

使用一组学生及其各自的 GPA 对 GPA 进行各种计算

使用 Python 从目录中读取所有 csv 文件