[英]Can't subtract dates because IDE considers one to be unicode
我正在比较两天,一是今天的日期,一是很大的清单。 IDE似乎认为item [3]是一个Unicode日期,因此我添加了print语句以显示结果。 该代码是大量代码的一部分,但我包括了重要的部分。
码:
print today
print item[3]
if (today - item[3]).days > 10:
item[4] = today + timedelta(days=10)
这是print语句的结果:
2015-01-11 00:00:00
2015-01-06
错误代码:
if (today - item[3]).days > Configuration_SLA:
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'unicode'
由于item[3]
是一个字符串(具有Unicode的说服力),请使用.strptime
将其转换为datetime.datetime
对象:
theday = datetime.datetime.strptime(item[3], '%Y-%m-%d')
if (today - theday).days > 10:
# etc, etc
我不认为您的IDE与它有任何关系-看起来它只是在传递来自Python本身的正确错误消息(如果您尝试在datetime.datetime
实例与任何类型的算术之间进行算术运算,则会出现该错误消息串!)。
顺便说一句,当您想查看看起来不像它们的东西时,请务必使用
print repr(thefunnything)
这将给你更多的信息, 永远只是
print thefunnything
其任务只是显示人类可读的字符串,无助于调试:-)
您看到的数字并不意味着它是datetime
对象或整数。 例如,
>>> a="4"
>>> print (a)
4
>>> type(a)
<class 'str'>
>>>
您会看到4
但这并不意味着4在这里是一个整数,它是字符串。 因此,您应该检查您的列表,像这样调试它;
print (type(item[3]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.