获取当前的年、月、日、时、分、秒,并将这些信息用作保存 Excel 文件的前缀

要获取当前的年、月、日、时、分、秒,并将这些信息用作保存 Excel 文件的前缀,你可以使用 Python 的 datetime 模块来获取当前时间,并格式化时间字符串,然后使用 pandas 库将数据保存为 Excel 文件。示例代码:

python 复制代码
from datetime import datetime
import pandas as pd

# 假设你有一个 DataFrame 'df' 需要保存为 Excel
# df = pd.DataFrame(...)  # 你的数据

# 获取当前时间
now = datetime.now()

# 格式化时间字符串作为文件名的一部分
# 例如: "20240409_1227"
filename_prefix = now.strftime("%Y%m%d_%H%M")

# 构建完整的文件名
filename = f"{filename_prefix}_your_data.xlsx"

# 指定保存路径(根据需要修改)
save_path = 'path/to/save/' + filename

# 将 DataFrame 保存为 Excel 文件
df.to_excel(save_path, index=False)

print(f"文件已保存为: {save_path}")

now.strftime("%Y%m%d_%H%M") 使用了 strftime 方法来按照指定的格式("%Y%m%d_%H%M")格式化当前时间字符串,其中 %Y 代表四位数的年份,%m 代表月份,%d 代表日,%H 代表小时(24小时制),%M 代表分钟。这样可以确保生成的时间字符串符合你的要求。

相关推荐
yivifu5 小时前
Excel表格取多行数据中的唯一值及多条件数据查询问题
excel
子夜江寒6 小时前
Python 学习-Day9-pandas数据导入导出操作
python·学习·pandas
咚咚王者10 小时前
人工智能之数据分析 Pandas:第七章 相关性分析
人工智能·数据分析·pandas
天一生水water11 小时前
顶级多项目管理 Excel 模板
excel·敏捷流程
咚咚王者11 小时前
人工智能之数据分析 Pandas:第六章 数据清洗
人工智能·数据分析·pandas
取啥都被占用11 小时前
VBA的excel逐行替换到word模板及打印还原
excel·vba
坚定信念,勇往无前12 小时前
vue3图片,pdf,word,excel,ppt多格式文件预览组件Vue Doc Viewers Plus
pdf·word·excel
Maxwell_li11 天前
Pandas 描述分析和分组分析学习文档
学习·数据分析·numpy·pandas·matplotlib
weixin_462446231 天前
【原创实践】python 获取节假日列表 并保存为excel
数据库·python·excel
Maxwell_li11 天前
pandas数据合并
机器学习·数据分析·numpy·pandas·matplotlib