如何保存 pandas csv 文件,其中包含精确到秒的确切日期

How do you save a pandas csv file with the exact date leading down to the seconds

我正在尝试将我的 csv 文件保存为今天的日期,格式如下: 'Wed Dec 9 04:26:40 2020'.

我试过使用 time.ctime():

excelfilename = 'file' + TodaysDate + ".csv"
data.to_csv(excelfilename , index=False)

但我收到错误消息:

OSError: [Errno 22] Invalid argument: 'file Dec  5 17:38:58 2020.csv'

还有其他解决方法吗?

我认为您不能在 Mac 或 Windows OS.

的文件名中使用 :

试试这个:

import pandas as pd
from datetime import datetime
df = pd.read_csv('<your original file here>')
now_ = datetime.now().strftime('%b %d %y %H_%M_%S')
df.to_csv('file ' + now_ + '.csv', index=False)

文件将以此名称导出:

print('file ' + datetime.now().strftime('%b %d %y %H_%M_%S') + '.csv')`
>>> file Dec 05 20 12_09_26.csv