【发布时间】:2012-03-23 09:44:12
【问题描述】:
我需要将日期转换为我正在编写的数据处理脚本的 Excel 序列号。通过使用我的 OpenOffice Calc 工作簿中的日期,我能够推断出 '1-Jan 1899 00:00:00' 映射到数字零。
我写了下面的函数来将python日期时间对象转换成Excel序列号:
def excel_date(date1):
temp=dt.datetime.strptime('18990101', '%Y%m%d')
delta=date1-temp
total_seconds = delta.days * 86400 + delta.seconds
return total_seconds
但是,当我尝试一些示例日期时,这些数字与我在 Excel(以及 OpenOffice Calc)中将日期格式化为数字时得到的数字不同。例如,在 Python 中测试 '2009-03-20' 得到 3478032000,而 excel 将序列号呈现为 39892。
上面的公式有什么问题?
*注意:我使用的是 Python 2.6.3,所以无权访问 datetime.total_seconds()
【问题讨论】:
标签: python excel datetime data-munging