我正在尝试处理一个文本文件,但我一直将我的变量分配给错误的数据

I'm trying to process a text file and I keep getting my variables assigned to the wrong data

我正在做一项作业,我必须打开输入和输出文件,读取数据并进行处理,然后将其写入输出文件。该代码应该根据学生是本科生还是研究生为他们分配字母等级,然后将他们的姓名和成绩写入新文件。我一直 运行 一个问题,在我输入文件名后,变量赋值发生了变化,我得到一个 ValueError 告诉我字符串不是整数。我的猜测是我的 for 循环正在接受一个变量并将所有内容向下移动,但我不知道如何解决这个问题。

# prompt the user to enter an input filename
try:
    filename_one = input('Please enter the name of the input data file: ')

# if the file is invalid, print an error message and prompt the user to enter another filename
except FileNotFoundError:
    print('Unknown input filename detected ' + filename_one + '.')

filename_two = input('Please enter the name of the output data file: ')

input_file = open(filename_one, 'r')
output_file = open(filename_two, 'w')

for line in input_file:
    # assign variables to the data from the input file
    under_or_grad = input_file.readline()
    name = input_file.readline()
    grade = int(input_file.readline())

    # write student name to output file
    output_file.write(name)

    # figure out if the student is an undergrad or a grad
    # assign grade to student
    if under_or_grad == 'GRAD':
        if 95 <= grade <= 100:
            grade = 'H'
        elif 80 <= grade <= 94:
            grade = 'P'
        elif 70 <= grade <= 79:
            grade = 'L'
        elif 0 <= grade <= 69:
            grade = 'F'
    elif under_or_grad == 'UNDERGRAD':
        if 90 <= grade <= 100:
            grade = 'A'
        elif 80 <= grade <= 89:
            grade = 'B'
        elif 70 <= grade <= 79:
            grade = 'C'
        elif 60 <= grade <= 69:
            grade = 'D'
        elif 0 <= grade <= 59:
            grade = 'F'

    # write output grade to output file
output_file.write(str(grade))
next_line = input_file.readline()

output_file.close()

这是我正在处理的数据示例(每个数据之间没有空行):

毕业

老简

76

我试图让它写入输出文件(相同的空行):

老简

L

以下是错误消息以防万一:

grade = int(input_file.readline())
ValueError: invalid literal for int() with base 10: 'GRAD\n'

您正在混合使用不同的文件读取技术:for line in input_filereadline。查看阅读文本文件的不同方式 herewhile True 循环中的 readline 可能最适合您的情况。您需要将 for line in input_file 替换为 while True,并可能删除底部的 next_line=input_file.readline()(除非不同学生的记录之间用空行分隔)。此外,您需要给 output_file.write 一个适当的缩进(如果需要, next_line=input_file.readline() ),因为现在它们不在循环内:

while True:
    # assign variables to the data from the input file
    under_or_grad = input_file.readline()
    if not under_or_grad:
        break
    name = input_file.readline()
    if not name:
        break
    grade_str = input_file.readline()
    if not grade_str:
        break
    grade = int(grade)

    # write student name to output file
    output_file.write(name)

    # figure out if the student is an undergrad or a grad
    # assign grade to student
    if under_or_grad == 'GRAD':
        if 95 <= grade <= 100:
            grade = 'H'
        elif 80 <= grade <= 94:
            grade = 'P'
        elif 70 <= grade <= 79:
            grade = 'L'
        elif 0 <= grade <= 69:
            grade = 'F'
    elif under_or_grad == 'UNDERGRAD':
        if 90 <= grade <= 100:
            grade = 'A'
        elif 80 <= grade <= 89:
            grade = 'B'
        elif 70 <= grade <= 79:
            grade = 'C'
        elif 60 <= grade <= 69:
            grade = 'D'
        elif 0 <= grade <= 59:
            grade = 'F'

    # write output grade to output file
    output_file.write(str(grade))
    # Use this only if there is a blank line between records
    next_line = input_file.readline()
    if not next_line:
         break