我正在尝试将自定义的文件名变量传递给ExcelWriter,但由于某些原因无法使ExcelWriter部分工作。如果我在ExcelWriter函数中将“Sheetname”替换为“Temp.xlsx”,则此方法有效,但我无法将变量传递给函数。每次运行脚本时,我都需要能够在文件名中存储今天的日期。
Spreadsheet = pandas.DataFrame()
Sheetname = 'Makino Machine Metrics ' + time.strftime("%m/%d/%Y") +'.xlsx'
writer = pandas.ExcelWriter(Sheetname, engine = 'xlsxwriter')
Spreadsheet.to_excel(writer, sheet_name= 'Results',index = False)
workbook = writer.book
worksheet = writer.sheets['Results']
writer.save()
先谢谢你的帮助
答案 0 :(得分:1)
当我运行你的程序时,我收到以下错误:
IOError: [Errno 2] No such file or directory:
'Makino Machine Metrics 07/12/2016.xlsx'
您可以通过从日期部分删除正斜杠来修复它:
import pandas
import time
Spreadsheet = pandas.DataFrame()
Sheetname = 'Makino Machine Metrics ' + time.strftime("%m_%d_%Y") +'.xlsx'
writer = pandas.ExcelWriter(Sheetname, engine = 'xlsxwriter')
Spreadsheet.to_excel(writer, sheet_name='Results', index=False)
workbook = writer.book
worksheet = writer.sheets['Results']
writer.save()
看起来错误来自于开放:
>>> open('12/12/12.xlsx', 'w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '12/12/12.xlsx'
因此,要么逃避这些字符,要么在日期中使用其他字符,如上例所示。