【问题标题】:Python Import csv file as dataframe with File chooserPython 使用文件选择器将 csv 文件导入为数据框
【发布时间】:2013-08-03 11:20:31
【问题描述】:

我想使用 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

【问题讨论】:

  • 什么“不起作用”是“don”为空?你有错误吗?
  • 对话框启动。当我点击 csv 文件时,什么都没有做,一旦关闭 gui,我有这个: ValueError Traceback (last recent call last) ValueError: Nothing can be done for type 目前。跨度>
  • 你在使用 rpy2 吗? (从代码中不清楚DataFrame是什么)

标签: csv python-2.7 dataframe filechooser


【解决方案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

【讨论】:

  • 是的,它适用于您的代码。是的,我用过 rpy2!非常感谢。但是我在显示数据框时得到了这个: Unable to unlink tempfile c:\users\~\appdata\local\temp\tmpzq4zao
  • 非常好 - 如果您指定您正在使用的库,将来它会有所帮助。请问我可以打勾吗? stackoverflow.com/help/someone-answers
猜你喜欢
  • 2023-01-15
  • 2017-03-29
  • 2013-02-19
  • 2013-05-06
  • 2010-12-09
  • 1970-01-01
  • 2011-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多