简体   繁体   English

使用to_csv时,将datetime添加到导出文件名

[英]Add datetime to the export file name when using to_csv

I try 我试试

snapshotdate = datetime.datetime.today().strftime('%m/%d/%Y')
os.chdir('/Users/Jason/Desktop')
df.to_csv(open(snapshotdata+'.csv','w'),index=none)

and get an error No such file or directory: '02/19/2017.csv'. 并得到一个错误没有这样的文件或目录:'02 /19/2017.csv'。 '02/19/2017.csv' is exactly what I want to get. '02 /19/2017.csv'正是我想要的。 How can I do it while using to_csv()? 我怎么能在使用to_csv()时这样做?

Don't use slashes in your filename. 不要在文件名中使用斜杠。 You're trying to create the file 2017.csv in subdirectory 19 of dir 02 , who don't exist. 您正在尝试在目录02的子目录19中创建文件2017.csv ,该文件不存在。

Yes. 是。 I change snapshotdate = datetime.datetime.today().strftime('%m/%d/%Y') to snapshotdate = datetime.datetime.today().strftime('%m-%d-%Y') 我将snapshotdate = datetime.datetime.today()。strftime('%m /%d /%Y')更改为snapshotdate = datetime.datetime.today()。strftime('%m-%d-%Y')

It works now. 它现在有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM