在python中有两个日期之间的小时数列表 [英] Have a list of hours between two dates in python

查看:84
本文介绍了在python中有两个日期之间的小时数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两次,我想使用Python中的相同格式列出它们之间的所有小时数

I have two times and I want to make a list of all the hours between them using the same format in Python

from= '2016-12-02T11:00:00.000Z'
to= '2017-06-06T07:00:00.000Z'
hours=to-from

所以结果将是这样的
2016-12-02T11:00:00.000Z
2016-12- 02T12:00:00.000Z
2016-12-02T13:00:00.000Z
.....以此类推
我该怎么办?应该使用哪种插件?

so the result will be something like this 2016-12-02T11:00:00.000Z 2016-12-02T12:00:00.000Z 2016-12-02T13:00:00.000Z ..... and so on How can I so this and what kind of plugin should I use?

推荐答案

如果可能的话,我建议您使用熊猫。

If possible I would recommend using pandas.

import pandas
time_range = pandas.date_range('2016-12-02T11:00:00.000Z', '2017-06-06T07:00:00.000Z', freq='H')

如果需要字符串,请使用以下命令:

If you need strings then use the following:

timestamps = [str(x) + 'Z' for x in time_range]
# Output
# ['2016-12-02 11:00:00+00:00Z',
#  '2016-12-02 12:00:00+00:00Z',
#  '2016-12-02 13:00:00+00:00Z',
#  '2016-12-02 14:00:00+00:00Z',
#  '2016-12-02 15:00:00+00:00Z',
#  '2016-12-02 16:00:00+00:00Z',
#  ...]

这篇关于在python中有两个日期之间的小时数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆