Python ValueError:需要 1 个以上的值才能解包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19749077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:29:57  来源:igfitidea点击:

ValueError: need more than 1 value to unpack

pythonpython-3.x

提问by user2899009

Ok, i know this topic has been addressed several times, but none of what i seen is helping me. I'm getting the error in the title and i'm not sure how to fix the error. Here is my code:

好的,我知道这个话题已经被多次提及,但我所看到的都没有帮助我。我收到标题中的错误,我不知道如何修复错误。这是我的代码:

def loadRecords():
    f = open("stu.txt", "r")
    students = f.readlines()
    f.close()
    return students

def addStudent():
    n = input("Enter student's name: ")
    ex1 = input("Enter Exam 1 grade: ")
    ex2 = input("Enter Exam 2 grade: ")
    ex3 = input("Enter Exam 3 grade: ")
    return n + " " + ex1 + " " + ex2 + " " + ex3 + "\n"

def displayStudents(students):
    for record in students:
        n, ex1, ex2, ex3 = record.split(",")
        ex1 = int(ex1)
        ex2 = int(ex2)
        ex3 = int(ex3)
        print("%-10s %5s    %5s    %5s" % (n, ex1, ex2, ex3))

def displayAvg(students):
    n = 1
    for record in students:
        n, ex1, ex2, ex3 = record.split(",")
        ex1 = int(ex1)
        ex2 = int(ex2)
        ex3 = int(ex3)
        avg = (ex1 + ex2 + ex3) / 3
        print("%-10s %5s" % (n, round(avg, 1)))
    n += 1

def saveRecords(students):
    f = open("stu.txt", "w")
    f.writelines(students)
    f.close

def main():
    students = loadRecords()

    while True:
        print("""                         
 Program Options. 
    1.) Display all contacts 
    2.) Create new contact
    3.) Display Averages
    4.) Save and exit 
    """)
        option = input("Enter 1, 2, or 3: ")
        print()

        if option == "1":
            displayStudents(students)
        elif option == "2":
            newRecord = addStudent()
            students.append(newRecord)
        elif option == "3":
            displayAvg(students)
        elif option == "4":
            saveRecords(students)
            break
        else:
            print("Not happening")

main()

Here is the error recieved:

这是收到的错误:

Traceback (most recent call last):
  File "C:/Python33/Program 4/pro4.py", line 65, in <module>
    main()
  File "C:/Python33/Program 4/pro4.py", line 53, in main
    displayStudents(students)
  File "C:/Python33/Program 4/pro4.py", line 16, in displayStudents
    n, ex1, ex2, ex3 = record.split(",")
ValueError: need more than 1 value to unpack

Here is the file I am using, use notepad if you want to run the code.

这是我正在使用的文件,如果要运行代码,请使用记事本。

sam wilson,98,80,73
sue green,92,98,74
sue adams,89,89,92
ron harris,90,87,100
linda tyler,76,72,88
dave smith,72,91,75
steve davis,88,92,84

回答by Martijn Pieters

You probably have at least one emptyline in your file (usually the last line); explicitly test for that:

您的文件中可能至少有一个行(通常是最后一行);明确测试:

for record in students:
    if not record.strip():
        continue
    n, ex1, ex2, ex3 = record.split(",")

You may want to look at the csvmoduleto read your student records with instead; you'd still need to skip empty lines but the comma-splitting is handled for you.

您可能需要查看csv模块来读取您的学生记录;您仍然需要跳过空行,但会为您处理逗号分割。