本文介绍了在条件之间使用条件从PYTHON datetimerange中的00.00-7.00AM(EST)获取网站的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
published_date = 7/11/2019 at 06:04 AM,7/11/2019 at 1:04 AM,7/11/2019 at 5:36 AM,7/11/2019 at 04:00 PM
所有时间均为EST。
我需要从 00.00-07.00 AM(EST)
的时间范围内获取数据。应跳过 7.00AM(EST)的时间)
All times are in EST. I need to fetch data from time range of 00.00 - 07.00 AM(EST)
.It should skip the time above 7.00AM(EST)
如果published_date.time()< = 7:00 AM
print(published_date.time())
if published_date.time() <= 7:00AM print(published_date.time())
实际结果:
published_date = 7/11/2019 at 06:04 AM,7/11/2019 at 1:04 AM,7/11/2019 at 5:36 AM,7/11/2019 at 04:00 PM
预期结果:
published_date = 7/11/2019 at 06:04 AM,7/11/2019 at 1:04 AM,7/11/2019 at 5:36 AM
推荐答案
您的日期格式不正确。因此,它将无法解析并正确比较它。您需要使用 datetime
库将字符串转换为日期并进行比较。
这是工作示例:
Your date are not in correct format. Hence it will not parse and compare it correct. You need to use datetime
library to convert the strings into date and compare. Here is the working example:
from datetime import datetime
import datetime as dt
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
checkTime = dt.time(7,0,0)
published_dates = ["7/11/2019 06:04 AM","7/11/2019 1:04 AM","7/11/2019 5:36 AM","7/11/2019 04:00 PM"]
filtered_dates = []
for d in published_dates:
dt1 = datetime.strptime(d, '%d/%m/%Y %I:%M %p')
if (dt1.time() < checkTime):
filtered_dates.append(dt1.strftime("%d/%m/%Y %I:%M %p"))
print('filtered_dates : ', filtered_dates)
这篇关于在条件之间使用条件从PYTHON datetimerange中的00.00-7.00AM(EST)获取网站的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!