2016-11-23 87 views
0

我對此問題有疑問,我的輸出應該如下所示。Python粘在字典中寫入文件的順序

輸出:

ID Exam1 Exam2 Homework Attendance Project1 Project2 Class Recap Final Grade Potential Grade 
32165487 10 70 50 40 100 80 50 100 D 
21321853 52 95 72 56 95 32 56 100 C+ 
41861235 95 12 47 32 68 92 35 100 D 
84534853 58 38 84 84 89 68 74 100 C 

代碼:

def generateReport(gradebook): 
    outfile=open('gradebook.txt','w') 
    currentstud=[] 

    for stu in d: 
     #print(stu) 
     currentstud.append(d[stu]) 
     whatever={} 
     lst1=[] 
    for students in currentstud: 
     print(students) 
     grades1=[] 
    for grades in students: 
     outfile.write(grades + '\t') 
    outfile.write('\n') 
    for val in students.values(): 

     outfile.write(val + '\t') 
    outfile.close 



    d={'37340752': {'exam1': '50', 'project1': '40', 'classrecap': '39', 'homework': '62', 'exam2': '3', 'attendance': '17', 'project2': '86', 'id': '37340752'}, 
     '95255664': {'exam1': '76', 'project1': '60', 'classrecap': '39', 'homework': '81', 'exam2': '57', 'attendance': '19', 'project2': '42', 'id': '95255664'}, 
     '47718233': {'exam1': '81', 'project1': '55', 'classrecap': '53', 'homework': '46', 'exam2': '46', 'attendance': '4', 'project2': '14', 'id': '47718233'}, 
     '55527760': {'exam1': '34', 'project1': '89', 'classrecap': '39', 'homework': '19', 'exam2': '99', 'attendance': '78', 'pta': 65, 'project2': '99', 'id': '55527760'}, 
     '32548926': {'exam1': '9', 'project1': '7', 'classrecap': '77', 'homework': '98', 'exam2': '1', 'attendance': '43', 'project2': '86', 'id': '32548926'}} 

    print(generateReport(d)) 

這是輸出我得到了上面的代碼。如何讓它打印所有行而不是一行?

project1 homework attendance id exam2 exam1 project2 classrecap 
55 46 4 47718233 46 81 14 53 
+1

您的帖子和代碼格式已關閉:請更正以使其可讀。 – sal

+1

在附註中,'outfile.close'不會關閉文件。你需要括號:'outfile.close()',或者(更好)使用'with'塊。 – zondo

回答

3

所有學生認沽等級(等級循環應該是裏面的學生)

def generateReport(gradebook): 
    outfile=open('gradebook.txt','w') 
    currentstud=[] 
    for stu in d: 
     currentstud.append(d[stu]) 
    for grades in currentstud[0]: 
     outfile.write(grades + '\t') 
    outfile.write('\n') 
    for students in currentstud: 
     for val in students.values(): 
      outfile.write(str(val) + '\t') 
     outfile.write('\n') 
    outfile.close 



d={'37340752': {'exam1': '50', 'project1': '40', 'classrecap': '39', 'homework': '62', 'exam2': '3', 'attendance': '17', 'project2': '86', 'id': '37340752'}, 
     '95255664': {'exam1': '76', 'project1': '60', 'classrecap': '39', 'homework': '81', 'exam2': '57', 'attendance': '19', 'project2': '42', 'id': '95255664'}, 
     '47718233': {'exam1': '81', 'project1': '55', 'classrecap': '53', 'homework': '46', 'exam2': '46', 'attendance': '4', 'project2': '14', 'id': '47718233'}, 
     '55527760': {'exam1': '34', 'project1': '89', 'classrecap': '39', 'homework': '19', 'exam2': '99', 'attendance': '78', 'pta': 65, 'project2': '99', 'id': '55527760'}, 
     '32548926': {'exam1': '9', 'project1': '7', 'classrecap': '77', 'homework': '98', 'exam2': '1', 'attendance': '43', 'project2': '86', 'id': '32548926'}} 

print(generateReport(d)) 
+0

與我得到多個時間[考勤\t項目2 \t classrecap \t PROJECT1 \t exam1 \t ID \t exam2 \t功課(4倍多) – mike

+0

看到的,編輯的代碼 – bharose

0

真的嗎?我得到的輸出是:

> TypeError: unsupported operand type(s) for +: 'int' and 'str' 

把你的代碼放到repl.it,就說明你的許多警告中的問題:

screenshot of this code in repl.it editor, highlighting the problem

你寫入文件的唯一地方是線14,和17,它們在環路for grades in students:for val in students.values()中,但是您沒有在任何地方定義students

您確實在上一個循環中將它用作循環變量,因此students具有前一循環的結束值。

要解決這個問題,您需要在成績簿上循環一次以獲得一個學生,然後對於每個條目,您需要遍歷學生的數據以處理文件的行。


關於「按順序」的標題是什麼?目前尚不清楚您是指應訂購列還是行或兩者,或訂單是什麼。但是當你可以使用有序的數據結構(一個字典)時,它會讓你感到非常愚蠢,因爲你可能會使用有序的數據結構(一個元組,一個列表),這會讓生活更輕鬆。 (您的數據中不存在哪些列,例如潛在成績?或者您希望的標題與您的數據的大寫/間距不同,Python的區分大小寫,所以這是您的數據中不存在的列煩人?或者csv模塊存在,可以爲你做很多這項工作)。

  1. 您將數據傳遞到generateReport名爲gradebook - 使用該名稱。當您使用d時,您將觸及功能,進入父級範圍,這是糾結和疑難的,這意味着gradebook永遠不會被使用,這對任何閱讀它的人來說都是令人困惑和無益的。

  2. 使用有用的名稱 - student_id來迭代學生ID,而不是stustudent單數而不是students。任何東西而不是whatever

  3. 然後用()關閉outfile來調用close方法。

  4. 並且不打印generateReport()的輸出...因爲沒有返回輸出來打印。

解決這一切讓我這樣的:列排序,套管作爲你的榜樣輸出,輸入還是作爲你的榜樣輸入,學生通過行ID分成順序。

def generateReport(gradebook): 
    outfile=open('gradebook.txt','w') 

    # all the columns, in the order they need to show up. 
    columns = ('ID', 'Exam1', 'Exam2', 'Homework', 'Attendance', 
      'Project1', 'Project2', 'Class Recap', 
      'Final Grade', 'Potential Grade') 

    # join the columns into a tab-separated line with a newline, and write it once 
    header_line = '\t'.join(columns) + '\n' 
    outfile.write(header_line) 

    # sort the student IDs into (numeric) order: 
    sorted_student_ids = sorted(gradebook.keys(), key=int) 

    # loop over each student ID 
    for student_id in sorted_student_ids: 

     # for each student, get their data 
     student_data = gradebook[student_id] 

     # build up a line of output for this students, using the columns 
     student_row = [] 
     for column in columns: 

     # columns don't match the dictionary keys :| 
     # remove spaces and make them lowercase, so they do 
     # also handle where there is no data for a column and get '' instead 
     # and convert to strings for writing to the file 
     column = column.replace(' ', '').lower() 
     value = str(student_data.get(column, '')) 

     student_row.append(value) 

     # now we have the data for this student, in the desired column order 
     student_line = '\t'.join(student_row) + '\n' 
     outfile.write(student_line) 

    outfile.close() 


d={'37340752': {'exam1': '50', 'project1': '40', 'classrecap': '39', 'homework': '62', 'exam2': '3', 'attendance': '17', 'project2': '86', 'id': '37340752'}, 
    '95255664': {'exam1': '76', 'project1': '60', 'classrecap': '39', 'homework': '81', 'exam2': '57', 'attendance': '19', 'project2': '42', 'id': '95255664'}, 
    '47718233': {'exam1': '81', 'project1': '55', 'classrecap': '53', 'homework': '46', 'exam2': '46', 'attendance': '4', 'project2': '14', 'id': '47718233'}, 
    '55527760': {'exam1': '34', 'project1': '89', 'classrecap': '39', 'homework': '19', 'exam2': '99', 'attendance': '78', 'pta': 65, 'project2': '99', 'id': '55527760'}, 
    '32548926': {'exam1': '9', 'project1': '7', 'classrecap': '77', 'homework': '98', 'exam2': '1', 'attendance': '43', 'project2': '86', 'id': '32548926'}} 

generateReport(d) 
with open('gradebook.txt') as f: 
    x = f.read() 
    print(x) 

,你可以run online at repl.it,而且也使用CSV模塊以後版本,註釋。

  • 我與repl.it沒有任何關係,我只是喜歡它。