2014-10-02 58 views
1

我需要在单个def中创建一个程序,打开一个文本文件“成绩”,其中第一个,最后一个和成绩由逗号分隔。每一行都是一个单独的学生。然后显示学生和成绩以及班级平均水平。然后继续添加另一个学生并将其保存到文本文件中,同时包括旧学生。 我想我只是不明白python通过文本文件的方式。如果我将'行'注释掉,我会看到它会打印old_names,但它好像一切都没有了。当行没有被注释掉'old_names'没有被打印,这让我认为文件被关闭了?还是空的?但是一切仍然在txt文件中,因为它应该是。文本文件读写ValueError:需要超过1个值才能打包

目前我得到这个错误....这一点我敢肯定,告诉我,我是弱智有在“行”没有信息

File "D:\Dropbox\Dropbox\1Python\Batch Processinga\grades.py", line 45, in main 
    first_name[i], last_name[i], grades[i] = line.split(',') 
ValueError: need more than 1 value to unpack 

最终目标是把它给我当前学生姓名和成绩,平均分。然后添加一个学生,保存该学生和成绩档案。然后能够将文件备份到所有学生,包括新的文件,然后重新执行。 我为成为一个结局而道歉。

def main(): 
    #Declare variables 
    #List of strings: first_name, last_name 
    first_name = [] 
    last_name = [] 
    #List of floats: grades 
    grades = [] 
    #Float grade_avg, new_grade 
    grade_avg = new_grade = 0.0 
    #string new_student 
    new_student = '' 


    #Intro 
    print("Program displays information from a text file to") 
    print("display student first name, last name, grade and") 
    print("class average then allows user to enter another") 
    print("student.\t") 

    #Open file “grades.txt” for reading 
    infile = open("grades.txt","r") 

    lines = infile.readlines() 

    old_names = infile.read() 
    print(old_names) 

    #Write for loop for each line creating a list 
    for i in len(lines): 
     #read in line 
     line = infile.readline() 

     #Split data 
     first_name[i], last_name[i], grades[i] = line.split(',') 

     #convert grades to floats 
     grades[i] = float(grades[i]) 

    print(first_name, last_name, grades) 
    #close the file 
    infile.close() 

    #perform calculations for average 
    grade_avg = float(sum(grades)/len(grades)) 


    #display results 
    print("Name\t\t Grade") 
    print("----------------------") 
    for n in range(5): 
     print(first_name[n], last_name[n], "\t", grades[n]) 

    print('') 
    print('Average Grade:\t% 0.1f'%grade_avg) 

    #Prompt user for input of new student and grade 
    new_student = input('Please enter the First and Last name of new student:\n').title() 
    new_grade = eval(input("Please enter {}'s grade:".format(new_student))) 

    #Write new student and grade to grades.txt in same format as other records 
    new_student = new_student.split() 
    new_student = str(new_student[1] + ',' + new_student[0] + ',' + str(new_grade)) 

    outfile = open("grades.txt","w") 

    print(old_names, new_student ,file=outfile) 

    outfile.close()enter code here 

回答

2

Python中的文件对象有一个“文件指针”,它跟踪你已经从文件中读取的数据。当您拨打readreadlinereadlines时,它会使用它来知道从哪里开始寻找。调用readlines将文件指针一直移动到文件末尾;后续的读取调用将返回一个空字符串。这就解释了为什么你在line.split(',')行上得到ValueError。 line是一个空字符串,因此line.split(",")返回一个长度为0的列表,但是您需要一个长度为3的列表来完成您尝试的三次分配。

一旦得到lines列表,您就不需要再与infile对象进行交互。你已经有了所有的线路;你可以直接迭代它们。

#Write for loop for each line creating a list 
for line in lines: 
    columns = line.split(",") 
    first_name.append(columns[0]) 
    last_name.append(columns[1]) 
    grades.append(float(columns[2])) 

请注意,我使用append代替listName[i] = whatever。这是必要的,因为当您尝试分配尚不存在的索引时,Python列表不会自动调整自己的大小;你只会得到一个IndexError。另一方面,append将根据需要调整列表的大小。

+0

谢谢您的解释,我可能需要再读几遍,但我明白了。现在,如果我可以让文件在文本文件中写入old_names,他们是怎么样的,而不是像这样...... ['Mickey,Mouse,90 \ n','Jane,Doe,50 \ n','Minnie ,Mouse,95 \ n','Donald,Duck,80 \ n','Daffy,Duck,70 \ n'] Lars,Olrich,69 – Krill 2014-10-03 00:49:21

+0

用.join()再次修复。 – Krill 2014-10-03 01:55:53

相关问题