通过读取csv文件创建报告

时间:2018-02-27 21:17:25

标签: python python-3.x

我正在学校做一个“侧面项目”来测试用户,然后将他们的结果保存到报告(csv文件)中。到目前为止,我已经做了一个报告,我正在为(Fergus)进行测验的“客户”可以搜索特定用户并查看他们已经完成的测验中的进度,但这很好,但我正在努力第二份报告。

对于第二份报告,Fergus必须进入某个主题(数学,英语,科学等)和难度级别(简单,困难等)的主题,它应该吐出一份报告,说明取得的平均分数在这个测验。我试图通过将所有单个分数加起来然后根据人们进行特定测验的次数来找到平均值,但是在阅读文件时似乎存在问题,因为它只是将所有结果读取为零。这意味着当总得分即将除以测验的次数时,它会显示错误,说我试图除以零。

def quizReport():
    overall=0
    amount=0
    subjectInput=input("\nPlease enter the subject name of the quiz(zes) you wish to view: ")
    difficultyInput=input("Please enter the difficulty level of the quiz(zes) you wish to view: ")
    with open ('reportForFergusTwo.csv') as quizzReport:
        quizReportReader=csv.reader(quizzReport)
        for column in quizReportReader:
            if subjectInput == column[0]:
                if difficultyInput == column[1]:
                    if ('0') == column[2]:
                        amount=amount+1
                    if ('1') == column[2]:
                        overall=overall+1
                        amount=amount+1
                    if ('2') == column[2]:
                        overall=overall+2
                        amount=amount+1
                    if ('3') == column[2]:
                        overall=overall+3
                        amount=amount+1
                    if ('4') == column[2]:
                        overall=overall+4
                        amount=amount+1
                    if ('5') == column[2]:
                        overall=overall+5
                        amount=amount+1
                    if ('6') == column[2]:
                        overall=overall+6
                        amount=amount+1
                    if ('7') == column[2]:
                        overall=overall+7
                        amount=amount+1
                    if ('8') == column[2]:
                        overall=overall+8
                        amount=amount+1
                    if ('9') == column[2]:
                        overall=overall+9
                        amount=amount+1
                    if ('10') == column[2]:
                        overall=overall+10
                        amount=amount+1
        total=overall/amount

1 个答案:

答案 0 :(得分:0)

你有一个除以0,因为没有找到相应的条目,因此amount永远不会递增,所以你应该处理这种情况。 此外,您的代码可以简化很多,非常重复。

我无法测试,因为您没有提供csv文件的示例,但请告诉我您是否对此感到满意:

def quizReport():
    overall = 0
    amount = 0
    subjectInput=input("\nPlease enter the subject name of the quiz(zes) you wish to view: ")
    difficultyInput=input("Please enter the difficulty level of the quiz(zes) you wish to view: ")

    with open ('reportForFergusTwo.csv') as quizzReport:
        quizReportReader=csv.reader(quizzReport)
        for line in quizReportReader:
            if subjectInput == line[0].strip() and difficultyInput == line[1].strip():
                amount += 1
                overall += int(line[2])

        if amount == 0:
            print("No entry corresponding to this subject and difficulty")
        else:
            avg_score = overall / amount
            print("Average score: %f" % avg_score)

编辑:如果找不到预期的数据,则可能无法正确读取csv。在循环中显示line print以确保它符合您的预期。

一个可能的原因可能是分隔符:csv有时用逗号分隔,有时用分号分隔。这是csv.reader的可选参数。

编辑:在我的代码中添加了strip(),以防csv中的逗号后面有空格。