获取当前的年、月、日、时、分、秒,并将这些信息用作保存 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 代表分钟。这样可以确保生成的时间字符串符合你的要求。

相关推荐
百锦再9 小时前
python之路并不一马平川:带你踩坑Pandas
开发语言·python·pandas·pip·requests·tools·mircro
wtsolutions11 小时前
Understanding JSON Formats - What JSON to Excel Supports
json·excel
wtsolutions11 小时前
Advanced Features - Unlocking the Power of JSON to Excel Pro
linux·json·excel
fs哆哆13 小时前
VB.NET和VBA教程-如何查找Excel数据区域边界
excel
jieshenai13 小时前
Pandas 基本操作记录
pandas
小矮强13 小时前
Excel中根据年月日提取月日,并按月日进行排序
excel
开开心心_Every13 小时前
图片批量压缩工具:支持有损无损两种模式
python·游戏·微信·django·pdf·excel·语音识别
wtsolutions14 小时前
Real-World Use Cases - How Organizations Use JSON to Excel
json·excel
wtsolutions14 小时前
Introduction to JSON to Excel - The Ultimate Conversion Tool
json·excel
骆驼爱记录15 小时前
Python打包命令全攻略
自动化·word·excel·新人首发