|
|
|
|
| 请教can't compare datetime.date to str的错误提示 |
 请教can't compare datetime.date to str的错误提示 - syncon [ 2005-07-23 07:59 | 1,659 byte(s)]
 Re: 请教can't compare datetime.date to str的错误提示 - limodou [ 2005-07-23 09:09 | 182 byte(s)]
 Re: 请教can't compare datetime.date to str的错误提示 - syncon [ 2005-07-23 18:39 | 56 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
我的Python是2.4.1版本的
test.py文件内容如下:
#! /usr/local/python/bin/python
# -*- coding: gb2312 -*-
import MySQLdb
import datetime
last = (datetime.date.today() - datetime.timedelta(10)).strftime("%Y-%m-%d")
now = (datetime.date.today()).strftime("%Y-%m-%d")
conn = MySQLdb.connect (host = "localhost",
user = "root",
passwd = "888888",
db = "test")
cursor = conn.cursor ()
cursor.execute ("SELECT login_name,last_time FROM customer")
while (1):
row = cursor.fetchone ()
if row == None:
break
else:
if row[1] <= now:
print row[1]
elif row[1] > now:
print now
cursor.close ()
conn.close ()
其中last_date的格式为2005-07-20
执行结果如下:
# /usr/local/python/bin/python /usr/local/script/radius/test.py
Traceback (most recent call last):
File "/usr/local/script/radius/test.py", line 21, in ?
if row[1] <= now:
TypeError: can't compare datetime.date to str
不知为何两个变量不能比较??在Python2.2.3里就没有这个问题,不过Python2.2.3又没有datetime模块!请指教
再请问一下,利用time模块怎样获取10天前的日期?要求格式为2005-07-20
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
从数据库中得到的是一个datetime实例,它不是string对象,应该使用strftime来转一下再比较。
计算时间差你的代码不是有了吗?datetime.date.today() - datetime.timedelta(10)
|
|
|
----
|
|
[Original]
[Print]
[Top]
|
|
|