【问题标题】:How do you save a pandas csv file with the exact date leading down to the seconds你如何保存一个精确到秒的pandas csv文件
【发布时间】:2020-12-05 17:53:32
【问题描述】:
我正在尝试将我的 csv 文件保存为今天的日期,格式如下:
'2020 年 12 月 9 日星期三 04:26:40'。
我尝试过使用 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'
还有其他方法可以解决这个问题吗?
【问题讨论】:
标签:
pandas
csv
export-to-csv
【解决方案1】:
我认为您不能在 Mac 或 Windows 操作系统的文件名中使用 :。
试试这个:
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