[Tutor] How to print lines within two timestamp
Alan Gauld
alan.gauld at yahoo.co.uk
Fri Oct 26 13:45:17 EDT 2018
On 26/10/2018 12:33, Asad wrote:
> Hi ,
>
> Yes i have the code :
It woiyukld help us to help you if you provided some clues as to what it
was doing.
A good start would be some comments - especially around the regexes.
Don't make us parse them without some idea of what you are expecting.
Also moremeaningful variable names than h,j,k etc
> import re
> import datetime
> from datetime import timedelta
You don't appear to use timedelta?
> Header = "*****************************************************"
>
> f3 = open ( r"D:\QI\logA.txt", 'r' )
> string = f3.read ()
> regex = re.compile ( "\n" )
> st = regex.sub ( " ", string )
I suspect regular string methods would be simpler here.
> st1 = st.split ( " " )
>
> if re.search ('ERR-1:', st ):
> x = re.findall ( "(\w{3})\s+([0-9]{2})\s+(\d+):(\d+):(\d+)\s+(\d+)", st )
> j = x[0][0] + " "+ x[0][1]+" " + x[0][2] +":"+ x[0][3]+":" +
> x[0][4]+" " + x[0][5]
> h = x[1][0] + " "+ x[1][1]+" "+ x[1][2] +":" + x[1][3] +":"+
> x[1][4] +" "+ x[1][5]
I'm not sure what exactly this is doing, but I suspect
datetime.strftime might do it better.
> y = datetime.datetime.strptime ( j, '%b %d %H:%M:%S %Y' )
> print y
> k = datetime.datetime.strptime ( h, '%b %d %H:%M:%S %Y' )
> print k
>
> f4 = open ( r"D:\QI\logC11.txt", 'r' )
>
> string1 = f4.read ()
> reg = re.compile ( "\n" )
> newst = reg.sub ( " ", string1 )
> newst1 = newst.split ( " " )
>
> if re.search ( "ERR-2", newst ):
> a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", newst )
> for i in range ( len ( a ) ):
Would this not be simpler as
for result in a:
and use result instead of a[i]
> newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' )
> if newtime > y and newtime < k:
You should be able to write this as
if y < newtime < k:
> print "Install patch1"
>
> if re.search ( "ERR-3", newst ):
> a = re.findall ( "\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]", newst )
> for i in range ( len ( a ) ):
> newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' )
> if newtime > y and newtime < k:
> print newtime, y, k
> print "Install patch2"
>
> ==============================================================================================
>
> output i get :
Can you show us the full output? It should start with your header line?
> *Install patch1 - wrong solution
> 2018-10-22 10:21:23 2018-10-22 10:21:15 2018-10-22 10:21:25
> Install patch2 - correct solution *
>
>
> *It should have only searched between timestamps **2018-10-22 10:21:15
> 2018-10-22 10:21:25*
Going by the above output that's exactly what it did?
And it found 2018-10-22 10:21:23
I'm not clear what you expected.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list