I would like to import a csv file into python with FileChooser and display it as dataframe. Here is the code and it didn't work. Thanks for your kind help.

def get_open_filename(self):

    filename = None
    chooser = gtk.FileChooserDialog("Open File...", self.window,
                                    gtk.FILE_CHOOSER_ACTION_OPEN,
                                    (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, 
                                     gtk.STOCK_OPEN, gtk.RESPONSE_OK))

    response = chooser.run()
    if response == gtk.RESPONSE_OK: 
        with open(chooser.get_filename(), 'rb') as csvfile:
            don = DataFrame.from_csvfile(csvfile)           ## I am confused here !!!
            print don
    chooser.destroy()

    return filename
有帮助吗?

解决方案

I believe from_csv file takes a filename not a file, using these docs Try replacing

with open(chooser.get_filename(), 'rb') as csvfile:
    don = DataFrame.from_csvfile(csvfile)           ## I am confused here !!!
    print don

with

    don = DataFrame.from_csvfile(chooser.get_filename())
    print don
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top