【发布时间】:2021-01-29 12:18:05
【问题描述】:
我有一个日期和时间的列表。对于每个日期,我只想提取秒值。
这是我的脚本:
import datetime
format_string = "%Y-%m-%d %H:%M"
now = datetime.datetime.now()
date = ['2021-01-29 15:15', '2021-01-29 15:50', '2021-01-29 17:00']
date = [(datetime.datetime.strptime(i, format_string)-now) for i in date]
print(date)
这是输出:
[datetime.timedelta(seconds=7269, microseconds=211221), datetime.timedelta(seconds=9369, microseconds=211221), datetime.timedelta(seconds=13569, microseconds=211221)]
我只想要列表“数据”的三个元素的秒数
【问题讨论】:
标签: python-3.x python-datetime timedelta