通过读取csv fi创建报告

2024-09-29 21:38:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在学校做一个“侧边项目”,测试用户,然后将他们的结果保存到报告(csv文件)中。到目前为止,我已经做了一个报告,我正在做测试的客户(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

Tags: 文件ofcsvthe用户inputif报告
1条回答
网友
1楼 · 发布于 2024-09-29 21:38:55

因为找不到相应的条目,所以被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。在循环中用print显示line,以确保它是您所期望的。你知道吗

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


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

相关问题 更多 >

    热门问题