URN Logo
UNIX Resources » Linux » China Linux Forum » Python 编 程 » 8 » 请教can't compare datetime.date to str的错误提示
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
请教can't compare datetime.date to str的错误提示
 
 
 
Subject: 请教can't compare datetime.date to str的错误提示
Author: syncon    Posted: 2005-07-23 07:59    Length: 1,659 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]
Subject: Re: 请教can't compare datetime.date to str的错误提示
Author: limodou    Posted: 2005-07-23 09:09    Length: 182 byte(s)
[Original] [Print] [Top]
从数据库中得到的是一个datetime实例,它不是string对象,应该使用strftime来转一下再比较。
计算时间差你的代码不是有了吗?datetime.date.today() - datetime.timedelta(10)
----
[Original] [Print] [Top]
Subject: Re: 请教can't compare datetime.date to str的错误提示
Author: syncon    Posted: 2005-07-23 18:39    Length: 56 byte(s)
[Original] [Print] [Top]
非常感谢,终于解决了!
我是新手,大家多多关照!
[Original] [Print] [Top]
« Previous thread
如何快速的创建一个比较大的数组?
Python 编 程
8
Next thread »
python程序中如果具有root用户权限
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 04:11:08, cost 0.089160919189453 ms.