TypeError:无法比较datetime.timedelta到float [英] TypeError: can't compare datetime.timedelta to float

查看:120
本文介绍了TypeError:无法比较datetime.timedelta到float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python脚本来计算开始日期和结束日期格式之间的持续时间,例如 20140520160000 20140520170000 ,这样我就可以开始工作了。

I'm working on my python script to work out the duration times between start date and end date format like 20140520160000 and 20140520170000 so I can get the hour.

我在使用此代码时遇到了麻烦:

I'm having a trouble with this code:

if epgDuration >= 0.10 and epgDuration <= 0.30:
   epgwidth = "250"

当我尝试比较0.10分钟和0.30分钟之间的时间范围时,我得到一个错误。

I get an error when I'm trying to compare the range of the times between 0.10 mins and 0.30 mins.

我得到的错误是:TypeError:无法比较datetime.timedelta浮动。

The error I get is: TypeError: can't compare datetime.timedelta to float.

错误在此行上跳跃:

if epgDuration >= 0.10 and epgDuration <= 0.30:

结果如下:

14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 2:30:00
14:44:55 T:1580  NOTICE: 3:00:00
14:44:55 T:1580  NOTICE: 1:00:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 1:00:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00

以下是我用来设定时间的代码:

Here is the code when I use to duration the times:

for row in programs:
    program_startdate = str(row[2])
    program_endDate = str(row[3])

    try:
       start_date = datetime.datetime.strptime(program_startdate, "%Y%m%d%H%M%S")
       end_date = datetime.datetime.strptime(program_endDate, "%Y%m%d%H%M%S")
    except TypeError:
       start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(program_startdate, "%Y%m%d%H%M%S")))
       end_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(program_endDate, "%Y%m%d%H%M%S")))

    #workout the duration times to get the program time
    epgDuration = end_date - start_date

    if epgDuration >= 0.10 and epgDuration <= 0.30:
       epgwidth = "250"

    elif epgDuration >= 1.00 and epgDuration <= 1.29:
         epgwidth = "500"
    print epgwidth


推荐答案

实际上,您不能将 timedelta 与浮点值进行比较。

Indeed, you cannot compare a timedelta to a float value.

可以将对象转换为秒:

if 600 <= epgDuration.total_seconds() <= 1800:

其中10分钟是600秒,而30分钟是1800。

where 10 minutes is 600 seconds, and 30 minutes is 1800.

或创建新的 timedelta()对象与以下对象进行比较:

Or create new timedelta() objects to compare against:

epgwidth = "0"

if timedelta(minutes=10) <= epgDuration <= timedelta(minutes=30):
    epgwidth = "250"

elif timedelta(hours=1) <= epgDuration <= timedelta(hours=1.5):
    epgwidth = "500"

我给了 epgwidth if 语句之前的默认值,以防时差不落在10-30分钟或1- 1.5小时范围。

I've given epgwidth a default value before the if statements for the case where the time difference is not falling in the 10-30 minutes or 1-1.5 hour ranges.

这篇关于TypeError:无法比较datetime.timedelta到float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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