python - bool 值比较日期时的极其奇怪的行为

标签 python

尝试在 Python 中比较日期时,我注意到一些非常奇怪的行为。我正在尝试使用日期来检查是否将项目添加到 MySQL 数据库(使用 mysqldb 库),并且在比较日期时,我遇到了奇怪的行为:

代码是:

   latestDate = pullDateFromTable() #pull the latest item from the table
   for transaction in transactions:
        print "Transaction Time:",transaction.date
        print "Latest Entry:",latestDate
        if transaction.date > latestDate:
            print "Transaction is new, added product"
            #insert item to MySQL database
        else:
            print "Old transaction, did not add product"

输出是:

Transaction Time: Thu Oct  4 11:42:25 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Old transaction, did not add product

Transaction Time: Wed Oct  3 22:11:48 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 19:32:56 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 17:46:35 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 17:46:35 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 17:46:35 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 16:25:06 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 14:00:48 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 11:46:20 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 11:46:20 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 11:22:37 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 09:05:13 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 09:05:13 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 09:05:13 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Tue Oct  2 07:38:25 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Mon Oct  1 17:39:52 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Old transaction, did not add product

Transaction Time: Mon Oct  1 15:05:29 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Old transaction, did not add product

Transaction Time: Sun Sep 30 20:56:38 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Old transaction, did not add product

Transaction Time: Sun Sep 30 09:52:09 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Old transaction, did not add product

您会注意到周二或周三(10 月 2 日和 3 日)的日期大于最晚的日期(10 月 4 日),但 9 月或 10 月 1 日的日期小于。

对为什么会这样有任何想法吗?

最佳答案

您的日期似乎是字符串,并且正在按字母顺序进行比较。 WT 之后。

关于python - bool 值比较日期时的极其奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12733897/

相关文章:

python - 尝试将 R 库与 rpy2 一起使用时出错

python - 序列输出循环 - python

python - !r 在 Python 中是什么意思?

python - 为什么 Python 3 打印结束 ""在解释器中产生异常而不是从导入中产生异常?

python - 剥离动态 url 内容

python - 如何使用 Neupy 获取神经元表示的点

python - 在 fedora 23 上安装 pip install psycopg2 时出错

python - Django LiveServer测试用例 : disable SSL at test-time

python - Pandas 范围日期极度减慢功能

python - 使用python讲解onehotencoder