Python使用File选择器将csv文件导入为数据帧

时间:2013-08-02 08:51:34

标签: csv python-2.7 dataframe filechooser

我想用FileChooser将csv文件导入python并将其显​​示为数据帧。这是代码,它没有用。谢谢你的帮助。

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

1 个答案:

答案 0 :(得分:1)

我相信from_csv文件使用文件名而不是文件,使用这些docs 尝试替换

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

    don = DataFrame.from_csvfile(chooser.get_filename())
    print don