在Python中读取.csv - 错误列表索引超出范围

时间:2014-03-24 21:01:30

标签: python csv tkinter

我试图通过TkInter显示存储在.csv文件中的信息。 CSV文件名为questionnaire.csv,与QuestionnaireDisplayResults.py文件位于同一目录中,该文件用于显示存储的信息。当我执行QuestionnaireDisplayResults.py时,

我收到以下错误:

Traceback (most recent call last):
  File "Z:\group project\QuestionnaireDisplayResults.py", line 69, in <module>
    main()
  File "Z:\group project\QuestionnaireDisplayResults.py", line 64, in main
    app = DisplayResults(root)
  File "Z:\group project\QuestionnaireDisplayResults.py", line 14, in __init__
    self.retrieveResponse(scrollbar)
  File "Z:\group project\QuestionnaireDisplayResults.py", line 32, in retrieveResponse
    firstName = row[0]
IndexError: list index out of range

.csv文件中的信息:

as,as,Buisness Information Systems
iuoioiu,iuooiiuooiu,Buisness Information Systems
joe,blogg,Buisness Information Systems

代码:

from Tkinter import *
import csv

class DisplayResults(Frame):
# GUI Setup
    def __init__(self, master):
    # Initialise Questionnaire Class

        Frame.__init__(self, master)
        scrollbar = Scrollbar(master)
        scrollbar.pack(side=RIGHT, fill=Y)      

        self.pack()
        self.retrieveResponse(scrollbar)
        self.centre_window()

    def retrieveResponse(self,scrollbar):

        self.txtDisplay = Text(self, height=14, width=100, bg='#CDD1CD', yscrollcommand=scrollbar.set)      # Initialise text box
        scrollbar.config(command=self.txtDisplay.yview)
        self.txtDisplay.tag_configure('title', font=('MS', 14, 'bold'), underline=1)    # Initialise the title text style
        self.txtDisplay.tag_configure('data', font=('MS', 12))      # initialise the details text style

        tab_results = ("\t" + "\t" + "\t")          # Initialise a variable for style tabulation 

        self.txtDisplay.insert(END,"\t" + "Forename" + tab_results + "Surname"
                                + tab_results + "Suggested Degree" + "\n", 'title')     # Insert the title text 

        with open('questionnaire.csv', 'rb') as f:
            reader = csv.reader(f)
            for row in reader:
                firstName = row[0]
                lastName = row[1]
                degree = row[2]

                self.txtDisplay.insert(END,"\t" + firstName + tab_results + lastName
                                        + tab_results + degree + "\n", 'data')  # Insert the details into the text box

        self.txtDisplay['state'] = DISABLED
        self.txtDisplay.pack()


    def centre_window(self):

        width = 750
        height = 225

        swidth = self.master.winfo_screenwidth()
        sheight = self.master.winfo_screenheight()

        x = (swidth - width)/2
        y = (sheight - height)/2
        self.master.geometry('%dx%d+%d+%d' % (width, height, x, y))




def main():
    # Main
    root = Toplevel()
    root.minsize(750, 225)
    root.maxsize(750, 225)
    root.title("Questionnaire Results")
    app = DisplayResults(root)
    root.mainloop()


if __name__ == '__main__':
    main()

0 个答案:

没有答案