【发布时间】:2015-10-12 14:58:47
【问题描述】:
更新
感谢大家的建议,但我在这里遇到了一个新问题。
由于我正在比较两个 datetime.datetime 对象,但我没有意识到它没有属性 items 或 keys 可供迭代,因此下面提供的一些有效答案不再有效。我在这里重构我的虚拟数据以更好地反映我的用例,
# Two datetime that I want to assert equal as long as they are equal to the 'second'
now = datetime.datetime(2015, 7, 22, 11, 36, 49, 811000)
then = datetime.datetime(2015, 7, 22, 11, 36, 49, 811099)
assert now == then # this for sure will return false
从示例中可以看出,除“微秒”之外的每个属性都是相等的。无论如何要遍历属性并进行比较?
谢谢各位!
【问题讨论】:
-
除了任何一个都需要吗?还是除了特定的一个之外的所有?
-
除一个特定的,本例
attr3 -
你看过这个:stackoverflow.com/questions/7999935/… -- 试试
assert now.strftime("%Y-%m-%d %H:%M:%S") == then.strftime("%Y-%m-%d %H:%M:%S") -
@xnx 成功了!我只需要声明
then.strftime("%Y-%m-%d %H:%M:%S") == now.strftime("%Y-%m-%d %H:%M:%S")!如果有人想要断言等于天、小时、分钟等,这也可能很有用。请在下面编辑您的答案,我会将其作为正确答案。谢谢! -
用另一个建议编辑了我的答案。