在熊猫中写入Excel的问题

时间:2013-08-13 23:39:57

标签: pandas ipython

使用Pandas中的to_excel函数对ipython脚本产生以下恼人的问题:在脚本结束时,我想将结果写入Excel文件。如果我包含一个例如“〜/”的路径,则脚本会随附所附的错误消息而中断。如果我只输入文件名,则脚本运行并在当前文件夹中生成新文件。这是一个错误还是我做错了什么? 代码如下:

path=dir+file_name
print "Writing to %s " % path
opt_design.to_excel(path, sheet_name='sheet1',index=False)
print "Finished!"``

运行时错误是:

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-1-b9e6707b5b6c> in <module>()
 43 path=dir+file_name
 44 print "Writing to %s " % path
---> 45 opt_design.to_excel(path, sheet_name='sheet1',index=False)
 46 print "Finished!"
 47 

/Library/Python/2.7/site-packages/pandas-0.11.1.dev_fcced51_20130617-py2.7-macosx-10.8- intel.egg / pandas / core / frame.pyc in_excel(self,excel_writer,sheet_name, na_rep,float_format,cols,header,index,index_label,startrow,startcol)

1494                                  startrow=startrow, startcol=startcol)
1495         if need_save:
-> 1496             excel_writer.save()
1497 
1498     def to_stata(self, fname, convert_dates=None, write_index=True, encoding="latin-1",
保存(自我)

/Library/Python/2.7/site-packages/pandas-0.11.1.dev_fcced51_20130617-py2.7-macosx-10.8-intel.egg/pandas/io/excel.pyc

 351         Save workbook to disk
 352         """
 --> 353         self.book.save(self.path)
 354 
 355     def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):

/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/openpyxl/workbook.pyc in save(self,filename)

 212             save_dump(self, filename)
 213         else:
 --> 214             save_workbook(self, filename)
save_workbook中的 /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/openpyxl/writer/excel.pyc(工作簿,文件名)

 153     """
 154     writer = ExcelWriter(workbook)
 --> 155     writer.save(filename)
 156     return True
 157 
保存中的 /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/openpyxl/writer/excel.pyc(self,filename)

 135     def save(self, filename):
 136         """Write data into the archive."""
 --> 137         archive = ZipFile(filename, 'w', ZIP_DEFLATED)
 138         self.write_data(archive)
 139         archive.close()
init 中的 /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.pyc(自我,文件,模式) ,compression,allowZip64)

 750             modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}
 751             try:
 --> 752                 self.fp = open(file, modeDict[mode])
 753             except IOError:
 754                 if mode == 'a':

IOError:[Errno 2]没有这样的文件或目录:'〜/ CloudStation / test1.xlsx'

1 个答案:

答案 0 :(得分:4)

Python不会为您扩展主目录。你有几个选择:

  1. os.path.expanduser(the_path)
  2. os.path.join(os.environ.get('HOME'), the_path)
  3. 这是一个例子

    In [17]: os.path.expanduser('~/a_path_wth_tilde')
    Out[17]: '/home/phillip/a_path_wth_tilde'