【发布时间】:2015-09-25 05:14:10
【问题描述】:
尝试使用以下代码编写to_csv:
file_name = time.strftime("Box_Office_Data_%Y/%m/%d_%H:%M.csv")
allFilms.to_csv(file_name)
但出现以下错误:
FileNotFoundError Traceback (most recent call last)
<ipython-input-36-aa2d6e13e9af> in <module>()
9
10 file_name = time.strftime("Box_Office_Data_%Y/%m/%d_%H:%M.csv")
---> 11 allFilms.to_csv(file_name)
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/frame.py in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, quoting, quotechar, line_terminator, chunksize, tupleize_cols, date_format, doublequote, escapechar, decimal, **kwds)
1187 escapechar=escapechar,
1188 decimal=decimal)
-> 1189 formatter.save()
1190
1191 if path_or_buf is None:
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/format.py in save(self)
1440 else:
1441 f = com._get_handle(self.path_or_buf, self.mode,
-> 1442 encoding=self.encoding)
1443 close = True
1444
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/common.py in _get_handle(path, mode, encoding, compression)
2827 f = open(path, mode, encoding=encoding)
2828 else:
-> 2829 f = open(path, mode, errors='replace')
2830 else:
2831 f = open(path, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'Box_Office_Data_2015/09/24_22:11.csv'
由于我正在写入 csv,为什么它会搜索尚未创建的文件/目录?
任何人的帮助将不胜感激:)
【问题讨论】:
-
文件名不合法。 / 表示目录,不确定:。将所有 / 和 : 替换为 - 或 _。
标签: python csv pandas export-to-csv