Python 日期时间比较不起作用
我试图比较当前日期和以前日期之间的日期和时间,它在IDE上工作,但不是从我的代码。谁能告诉我我做错了什么 我得到的错误是: 回溯(最近一次调用last):文件“read_log.py”,第37行,在 如果日志日期<当前时间,日志日期>最后时间: TypeError:无法将datetime.datetime与str进行比较Python 日期时间比较不起作用,python,python-2.7,Python,Python 2.7,我试图比较当前日期和以前日期之间的日期和时间,它在IDE上工作,但不是从我的代码。谁能告诉我我做错了什么 我得到的错误是: 回溯(最近一次调用last):文件“read_log.py”,第37行,在 如果日志日期最后时间: TypeError:无法将datetime.datetime与str进行比较 来自IDE的代码: Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]
来自IDE的代码:
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> current_time = datetime.datetime.now()
>>> print current_time.strftime('%b %d %H:%M:%S')
Feb 19 15:45:49
>>> c= current_time.strftime('%b %d %H:%M:%S')
>>> lastHourTime = datetime.datetime.now() - datetime.timedelta(hours = 1)
>>> l=lastHourTime.strftime('%b %d %H:%M:%S')
>>> print l
Feb 19 14:47:32
>>> print c
Feb 19 15:45:49
>>> print l
Feb 19 14:47:32
>>> l<c
True
>>> l>c
False
>>> m='Feb 7 07:33:19'
>>> m<l
True
>>>
Python 2.6.6(r266:8429220015年7月23日15:22:56)
[GCC 4.4.7 20120313(Red Hat 4.4.7-11)]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入日期时间
>>>当前时间=datetime.datetime.now()
>>>打印当前时间。strftime(“%b%d%H:%M:%S”)
二月十九日15:45:49
>>>c=当前时间。strftime(“%b%d%H:%M:%S”)
>>>lastHourTime=datetime.datetime.now()-datetime.timedelta(小时=1)
>>>l=lastHourTime.strftime(“%b%d%H:%M:%S”)
>>>打印l
二月十九日14:47:32
>>>打印c
二月十九日15:45:49
>>>打印l
二月十九日14:47:32
>>>l>>l>c
假的
>>>m='Feb 7 07:33:19'
>>>m>>
我的脚本代码:
get_date = re.compile('([A-Z-a-z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+)(.*)')
current_time = datetime.datetime.now()
lastHourTime = datetime.datetime.now() - datetime.timedelta(hours = 1)
log_file=_read_log()
for line in log_file:
match=re.search(get_date,line)
if match:
log_date= match.group(1).rstrip()
if log_date < current_time and log_date > lastHourTime :
print line
get_date=re.compile(“([A-Z-A-Z]+[0-9]+[0-9]+:[0-9]+:[0-9]+)(.*)”
当前时间=datetime.datetime.now()
lastHourTime=datetime.datetime.now()-datetime.timedelta(小时=1)
log\u file=\u read\u log()
对于日志文件中的行:
匹配=重新搜索(获取日期,行)
如果匹配:
log_date=match.group(1).rstrip()
如果日志日期<当前时间,日志日期>最后时间:
打印行
您正在将字符串(日志日期)与datetime.datetime(当前时间)进行比较
所以我想您应该将log_date转换为datetime:
import datetime as dt
d = 'Feb 7 07:33:19'
print(dt.datetime.strptime(d, "%b %d %H:%M:%S").replace(year=dt.date.today().year))
例如:
get_date = re.compile('([A-Z-a-z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+)(.*)')
current_time = datetime.datetime.now()
lastHourTime = datetime.datetime.now() - datetime.timedelta(hours = 1)
log_file=_read_log()
for line in log_file:
match=re.search(get_date,line)
if match:
log_date = datetime.datetime.strptime(match.group(1).rstrip(), "%b %d %H:%M:%S").replace(year=datetime.date.today().year)
if log_date < current_time and log_date > lastHourTime :
print line
get_date=re.compile(“([A-Z-A-Z]+[0-9]+[0-9]+:[0-9]+:[0-9]+)(.*)”
当前时间=datetime.datetime.now()
lastHourTime=datetime.datetime.now()-datetime.timedelta(小时=1)
log\u file=\u read\u log()
对于日志文件中的行:
匹配=重新搜索(获取日期,行)
如果匹配:
log_date=datetime.datetime.strtime(match.group(1).rstrip(),%b%d%H:%M:%S))。替换(year=datetime.date.today().year)
如果日志日期<当前时间,日志日期>最后时间:
打印行
Hi MAxu,这里我试图比较时间,因此如果我将log\u日期转换为您提供的输出,我不需要将当前时间和最后时间转换为与log\u日期相同的格式吗?Hi@alammd,不需要将log\u日期(作为字符串,因为“re.search().group()”函数的性质)转换为datetime.datetime,正如我在回答中所显示的那样。然后你会比较两个datetime.datetime的。嗨,Max,有一些小问题,一些日期比较不起作用,示例输出命令,我使用的正是你使用的命令,只有更改,我使用的一个子句是log_date当前时间2月20日10:46:21
日志日期2016-12-13 07:33:11
不匹配
12月13日07:33:11 xxxx yum[6315]:已安装:14:tcpdump-4.0.0-5.20090921gitdf3cb4.2.el6.x86_64
日志日期2016-01-21 15:03:20
匹配
一月21日15:03:20 xxxxx百胜[6352]:已安装:gnutls-2.8.5-19.el6_7.x86_64