在 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 to_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 in save(self)
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)
/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/openpyxl/writer/excel.pyc in save_workbook(工作簿,文件名)
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 in save(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()
/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.pyc in init (self, file, mode, 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'