2016-04-28 135 views
-3

如何在不使用re的情况下使用相同的逻辑。如何在不使用RE的情况下使用DateTime?

#!/usr/bin/env python 

#Import the regular expression 
import re 

print "The task Begins" 

print " The Begin time is at 03:00:00" 
#Set the beginning time and name it as starttime 
starttime="03:00:00" 

print "The End time is at 03:59:59" 
#Set the ending time and name it as endtime 
endtime="03:59:59" 

#Specify the time format 
time_re = re.compile(r'(\d+:\d+:\d+)') 

#Using the condition 
with open("abc.log", "r") as fh: 

for line in fh.readlines(): 
    match = time_re.search(line) 
    if match: 
     matchDate = match.group(1) 
     if matchDate >= starttime and matchDate <= endtime: 
      print match.string.strip() 

print "The task is completed" 

PS:使用日期时间和时间。

+0

究竟是与当前的方法的问题? –

+0

我们不会为您编写程序......问题是什么?你是编码的初学者吗? –

+0

是的男人。我想使用日期时间格式。我用正则表达式。我是一个新鲜的男人。帮帮我。 –

回答

0

你可以使用string.find(),但最好使用正则表达式。

+0

Aye Mate。我知道,但要求是使用日期时间 –

+0

您正在阅读.log文件并通过比较startTime和endTime得到结果。你的问题不清楚。你能告诉你日期时间的确切问题吗? DateTime用于操纵日期和时间,Re用于匹配操作。 – user2030113

+0

开始和结束时间是我为从日志文件中获取日志条目而设置的参数值。假设我有日志文件,我想查找开始时间和结束时间之间的日志条目。我不知道如何更好地解释它。敬请谅解。 –

0

导入从日期时间的日期时间输入的日期时间

进口重新

为12:00:00和4点59分59秒为DT1及DT2创建两个日期时间分别对象

DT1 =日期时间.strptime(“12:00:00”,“%H:%M:%S”)。time() dt2 = datetime.strptime(“04:59:59”,“%H:%M:%S “).time()

time_re = re.compile(r'(\ d +:\ d +:\ d +)')

在开线( “test.log中”, “R”): 匹配= time_re.search(线) 如果匹配: matchdate = match.group(1) dt_match = datetime.strptime(matchdate, '%H:%M:%S'。)时间() 如果dt_match> = DT1和dt_match < = DT2: 打印match.string.strip()

+0

这应该是一个自我回答?请更正您的代码格式。 – SiHa

相关问题