Pandas 在 Excel 中保存数据框期间将时间戳添加到文件名

Fel*_*lix 4 timestamp save pandas

当我以 Excel 格式(“*.xlsx”)保存数据框时,我想向 Excel 文件名添加时间戳。

如何在使用 Pandas 保存文件时以 Excel 格式保存时向数据框添加运行时间戳?谢谢

su7*_*u7k 5

您可以使用日期时间模块。它是Python标准库之一,你可以通过pd.datetime如下方式访问它。

import pandas as pd

writer = pd.ExcelWriter('output_{}.xlsx'.format(pd.datetime.today().strftime('%y%m%d-%H%M%S'))) 
df1.to_excel(writer,'Sheet1')
df2.to_excel(writer,'Sheet2') 
writer.save()
Run Code Online (Sandbox Code Playgroud)

详细信息可以查看格式代码表。