如何迭代文件中的数据?

时间:2021-10-31 19:26:53

I'm new to Python and I'm trying to figure out how to display different things from a source file. The assignment is to write a program that lets the user type in the name of a file containing test answers (A, B, C, or D) from a math class and then open that file, which I believe I have already done below. Each file has a different number of lines because each class has a different number of students. Each line contains a student's ID number and the answers they put for the test, separated by commas. Example:

我是Python的新手,我正在试图弄清楚如何从源文件中显示不同的东西。分配是编写一个程序,让用户从数学类中键入包含测试答案(A,B,C或D)的文件名,然后打开该文件,我相信我已经在下面做过了。每个文件都有不同的行数,因为每个班级的学生人数不同。每行包含学生的身份证号码和他们为考试提供的答案,以逗号分隔。例:

"N12345678,B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D"

The program should then "grade" the test by comparing the answers each student put with the answer key, which is

然后,程序应该通过比较每个学生放置的答案和答案密钥来“评分”测试

"B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D"

The program should also determine and print out the highest score out of all the students in the text file, the lowest score, and the mean, median, mode, and range of the scores.

该程序还应确定并打印出文本文件中所有学生的最高分,最低分,以及分数的均值,中位数,模式和范围。

I already have the part written to open the file and count the number of students, but I've been trying and failing to write the section that will grade each student's answers. Here is what I have so far:

我已经编写了打开文件并计算学生人数的部分,但我一直在尝试并且未能编写将对每个学生的答案进行评分的部分。这是我到目前为止:

file_name = input("Enter a class file to grade (class1.txt, class2.txt, etc): ")
try:
    outfile = open(file_name, 'r')
    print("Successfuly opened",file_name)
    print()
except:
    print("File does not exist.")

student_counter = 0
for line in outfile:
    data = (line.rstrip('\n'))
    print(data)
    student_counter = student_counter + 1

print(student_counter)

And here is what I've tried for grading each student's answers, but it doesn't seem to be working. Each correct answer is worth 4 points, each incorrect subtracts 1 from the total score, and each question left blank is worth 0 points.:

以下是我尝试对每个学生的答案进行评分,但似乎并没有起作用。每个正确答案值4分,每个错误从总分中减去1,每个问题留空是0分:

answers = ["B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D"]
total = 1
counter = 1

for line in outfile:
    student_counter = student_counter + 1
    text_line = line.split(',')
    if (text_line[counter] == answers[counter-1]):
        total = total+4
    elif text_line[counter] == ' ':
        total = total
        counter = counter +1
    else:
        total = total-1
print(total)

How do I edit my program to iterate over each line in the inputted file and display the highest score, lowest score, median, mode, and range?

如何编辑我的程序以迭代输入文件中的每一行并显示最高分,最低分,中位数,模式和范围?

2 个解决方案

#1


Python's split(delimiter) is your friend in this case. One possible solution is just iterating over the lines in file, splitting them and comparing them to key. zip function comes in handy: if you also split the key, you'll end up with two lists of the same length where one list contains the student's answers and second one correct answers.

在这种情况下,Python的split(分隔符)是你的朋友。一种可能的解决方案是迭代文件中的行,拆分它们并将它们与键进行比较。 zip功能派上用场:如果你也分开了键,你最终会得到两个相同长度的列表,其中一个列表包含学生的答案,第二个列表包含正确的答案。

zip creates a single list of tuples from these 2 lists, where each tuple contains student's answer and correct answer and you can simply compare these two.

zip从这两个列表中创建单个元组列表,其中每个元组包含学生的答案和正确答案,您可以简单地比较这两个元组。

In order to do some future calculations, easy way is saving the results to some outside structure and then doing the calculation. Code that does this can look like this:

为了进行未来的计算,简单的方法是将结果保存到某些外部结构,然后进行计算。执行此操作的代码可能如下所示:

key = 'B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D'.split(',')
students = {}

with open('grades', 'r') as f:
    for line in f:
        line_split = line.rstrip('\n').split(',')
        students[line_split[0]] = 0
        for key, answer in zip(key, line_split[1:]):
            if key == answer:
                students[line_split[0]] = students[line_split[0]] + 4
            else:
                students[line_split[0]] = students[line_split[0]] - 1

print students

which gives the output

它给出了输出

{'N12345678': 100}

for your example data.

为您的示例数据。

If you were to iterate over the results and do some calculations, you can do that by

如果你要迭代结果并进行一些计算,你可以这样做

for student, score in students:
    print score

but I'll leave the calculations to you so you can solve the assignment yourself.

但是我会把计算留给你,这样你就可以自己解决这个问题了。

#2


If you can use dictionaries, I suggest this code as an option:

如果您可以使用词典,我建议将此代码作为选项:

answers = ['B', 'A', 'D', 'D', 'C', 'B', 'D', 'A', 'C', 'C', 'D', 'B', 'A', 'B', 'A', 'C', 'B', 'D', 'A', 'C', 'A', 'A', 'B', 'D', 'D']
students = {}

# Example of a line from the outfile
outfile = ["N12345678,B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D"]

# This simulates the loop used to iterate over the textfile
for line in outfile:
    total = 0  # Total score for the student

    student_answers = line.split(',')  # The student and his answers

    # We enumerate each element of the student's answer sheet. We exclude his ID for obvious reasons
    for i, answer in enumerate(student_answers[1:]):
        if (answer == answers[i]):  #Correct answer
            total += 4
        elif text_line[counter] != answers[i]:  # Wrong answer
            total -= 1
        else:  # Blank answer, we pass to the next iteration
            pass

    # We finally asociate each student's ID to his score.
    students[student_answers[0]] = total

print(students)

#1


Python's split(delimiter) is your friend in this case. One possible solution is just iterating over the lines in file, splitting them and comparing them to key. zip function comes in handy: if you also split the key, you'll end up with two lists of the same length where one list contains the student's answers and second one correct answers.

在这种情况下,Python的split(分隔符)是你的朋友。一种可能的解决方案是迭代文件中的行,拆分它们并将它们与键进行比较。 zip功能派上用场:如果你也分开了键,你最终会得到两个相同长度的列表,其中一个列表包含学生的答案,第二个列表包含正确的答案。

zip creates a single list of tuples from these 2 lists, where each tuple contains student's answer and correct answer and you can simply compare these two.

zip从这两个列表中创建单个元组列表,其中每个元组包含学生的答案和正确答案,您可以简单地比较这两个元组。

In order to do some future calculations, easy way is saving the results to some outside structure and then doing the calculation. Code that does this can look like this:

为了进行未来的计算,简单的方法是将结果保存到某些外部结构,然后进行计算。执行此操作的代码可能如下所示:

key = 'B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D'.split(',')
students = {}

with open('grades', 'r') as f:
    for line in f:
        line_split = line.rstrip('\n').split(',')
        students[line_split[0]] = 0
        for key, answer in zip(key, line_split[1:]):
            if key == answer:
                students[line_split[0]] = students[line_split[0]] + 4
            else:
                students[line_split[0]] = students[line_split[0]] - 1

print students

which gives the output

它给出了输出

{'N12345678': 100}

for your example data.

为您的示例数据。

If you were to iterate over the results and do some calculations, you can do that by

如果你要迭代结果并进行一些计算,你可以这样做

for student, score in students:
    print score

but I'll leave the calculations to you so you can solve the assignment yourself.

但是我会把计算留给你,这样你就可以自己解决这个问题了。

#2


If you can use dictionaries, I suggest this code as an option:

如果您可以使用词典,我建议将此代码作为选项:

answers = ['B', 'A', 'D', 'D', 'C', 'B', 'D', 'A', 'C', 'C', 'D', 'B', 'A', 'B', 'A', 'C', 'B', 'D', 'A', 'C', 'A', 'A', 'B', 'D', 'D']
students = {}

# Example of a line from the outfile
outfile = ["N12345678,B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D"]

# This simulates the loop used to iterate over the textfile
for line in outfile:
    total = 0  # Total score for the student

    student_answers = line.split(',')  # The student and his answers

    # We enumerate each element of the student's answer sheet. We exclude his ID for obvious reasons
    for i, answer in enumerate(student_answers[1:]):
        if (answer == answers[i]):  #Correct answer
            total += 4
        elif text_line[counter] != answers[i]:  # Wrong answer
            total -= 1
        else:  # Blank answer, we pass to the next iteration
            pass

    # We finally asociate each student's ID to his score.
    students[student_answers[0]] = total

print(students)