[Tutor] (no subject)

Asad asad.hasan2004 at gmail.com
Sat Oct 27 05:28:34 EDT 2018


Hi All ,

      I found a logic :

1.initialize print flag to false
2. Save starting and ending times in variables
3. loop to read or process sequential lines in the file
  a. See if line contains time
  b. If it does, extract time
  c. Compare time in line to starting time
  d. If line time is greater than or equal to start time, set print flag to
true
  e. If line time is greater than ending time, set print flag to false
  f. If print flag is true print line
  g. End of loop — process next line

start_time = 2018-10-22 10:21:15
end_time = 2018-10-22 10:21:25

f4 = open ( r"logC11.txt", 'r' )

for line in f4.readlines():
  a = re.findall( r'\d\d/\d\d/\d\d\s[012][0-9]:[0-5][0-9]:[0-5][0-9]', line )
  print a
  #b = a.group(0)
  newtime = datetime.datetime.strptime ( a[0], '%m/%d/%y %H:%M:%S' )
  print newtime
  if newtime > y and newtime < k:
    flag = True
  else:
    flag = False
  if flag :
    print line
  else :
    continue


But it errors out :

C:\Python27\python.exe D:/QI/test_qopatch.py
Traceback (most recent call last):
2018-10-22 10:21:15
  File "D:/QI/test_qopatch.py", line 32, in <module>
2018-10-22 10:21:25
    newtime = datetime.datetime.strptime ( a[0], '%m/%d/%y %H:%M:%S' )
['04/26/18 06:11:52']
2018-04-26 06:11:52
[]
IndexError: list index out of range

Process finished with exit code 1


Please provide the code

Thanks,

On Sat, Oct 27, 2018 at 1:35 PM <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
> Today's Topics:
>
>    1. Re: How to print lines within two timestamp (Alan Gauld)
>    2. Re: Python Help (Alan Gauld)
>    3. Re: How to print lines within two timestamp (Peter Otten)
>    4. How to print lines within two timestamp (Asad)
>
>
>
> ---------- Forwarded message ----------
> From: Alan Gauld <alan.gauld at yahoo.co.uk>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Fri, 26 Oct 2018 23:30:09 +0100
> Subject: Re: [Tutor] How to print lines within two timestamp
> On 26/10/2018 18:45, Alan Gauld via Tutor wrote:
>
> > It woiyukld
>
> No idea what happened there. Should be "would" of course!
>
> --
> 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
>
>
>
>
>
>
> ---------- Forwarded message ----------
> From: Alan Gauld <alan.gauld at yahoo.co.uk>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Fri, 26 Oct 2018 23:34:17 +0100
> Subject: Re: [Tutor] Python Help
> On 26/10/2018 18:20, Adam Eyring wrote:
>
> > beef = (beefmeals * 15.95)
>
> Note that the parens here are completely redundant.
> They don't break anything but neither do they
> contribute anything.
>
> WE already have LISP(*) for those who love parens,
> no need for (so many of) them in Python
>
> (*)Lots of Irrelevant Silly Parentheses :))))
>
> --
> 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
>
>
>
>
>
>
> ---------- Forwarded message ----------
> From: Peter Otten <__peter__ at web.de>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Sat, 27 Oct 2018 06:07:43 +0200
> Subject: Re: [Tutor] How to print lines within two timestamp
> Alan Gauld via Tutor wrote:
>
> > On 26/10/2018 18:45, Alan Gauld via Tutor wrote:
> >
> >> It woiyukld
> >
> > No idea what happened there. Should be "would" of course!
>
> Of coiyukrse! Nobody thoiyukght otherwiiyske :)
>
>
>
>
>
> ---------- Forwarded message ----------
> From: Asad <asad.hasan2004 at gmail.com>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Sat, 27 Oct 2018 12:32:51 +0530
> Subject: [Tutor] How to print lines within two timestamp
> On Sat, Oct 27, 2018 at 4:01 AM <tutor-request at python.org> wrote:
>
> > Send Tutor mailing list submissions to
> >         tutor at python.org
> >
> > To subscribe or unsubscribe via the World Wide Web, visit
> >         https://mail.python.org/mailman/listinfo/tutor
> > or, via email, send a message with subject or body 'help' to
> >         tutor-request at python.org
> >
> > You can reach the person managing the list at
> >         tutor-owner at python.org
> >
> > When replying, please edit your Subject line so it is more specific
> > than "Re: Contents of Tutor digest..."
> > Today's Topics:
> >
> >    1. How to print lines within two timestamp (Asad)
> >    2. Re: How to print lines within two timestamp (Alan Gauld)
> >    3. Re: Python Help (Bob Gailer)
> >    4. Re: Python Help (Adam Eyring)
> >    5. Re: Python Help (Adam Eyring)
> >
> >
> >
> > ---------- Forwarded message ----------
> > From: Asad <asad.hasan2004 at gmail.com>
> > To: tutor at python.org
> > Cc:
> > Bcc:
> > Date: Fri, 26 Oct 2018 17:03:01 +0530
> > Subject: [Tutor] How to print lines within two timestamp
> > Hi ,
> >
> > Yes i have the code :
> >
> > import re
> > import datetime
> > from datetime import timedelta
> >
> > f3 = open ( r"D:\QI\logA.txt", 'r' )
> > string = f3.read ()
> > regex = re.compile ( "\n" )
> > st = regex.sub ( " ", string )
> > 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]
> >     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 ) ):
> >         newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S'
> )
> >         if newtime > y and 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 :
> >
> > *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*
> >
> > *in logC11.txt  what I am doing wrong please adice stuck on this for
> long.*
> >
> >
> >
> > ---------- Forwarded message ----------
> > From: Alan Gauld <alan.gauld at yahoo.co.uk>
> > To: tutor at python.org
> > Cc:
> > Bcc:
> > Date: Fri, 26 Oct 2018 18:45:17 +0100
> > Subject: Re: [Tutor] How to print lines within two timestamp
> > 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?
> >
>      Answer: Yes I remove from datetime import 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.
> > answer : can you please provide the code to replace above
> >
>
>
> > > 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.
> > Answer: Its extracting all the dates in the format :Oct 22 10:21:15 2018
>
>    from logA.txt and storing it because this   logA.txt  is primary file
> which has the start and end date of the incident .
>
>
> >
> > >     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:
> >
> >    Answer : Correct suggestion will try
>
>
> > 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:
> > Answer : Correct
> >
> > >            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?
> >
>    This is the complete output
>
>
> >
> > > *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.
> >
>
>    Code did as it should have done . I need to change the code according to
> my requirement .
>    My requirement is to start(y) and end timestamp (k) from logA.txt which
> the code is getting now correctly then open logC11.txt see the timestamp
> nearest to start(y) which its doing now
> 2018-10-22 10:21:23  and then look for errors(ERR-2 and ERR-3) in the lines
> in logC11.txt starting from  2018-10-22 10:21:23 and before the timestamp
> (k) ==> here i need help my code is searching for ERR-2 and ERR-3 occurance
> in logC11.txt anywhere in the file I want it to search in a specific window
> of  2018-10-22 10:21:23 and less than timestamp (k)
>
> so the my present coded has printed the output :
> *Install patch1   - wrong solution
> This it will print if it executes the following block :
>
> 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 ) ):
>         newtime = datetime.datetime.strptime ( a[i], '%m/%d/%y %H:%M:%S' )
>         if newtime > y and newtime < k:
>            print "Install patch1
>
> Now if you see ERR -2 in logC11.txt it occured at 26th april  instead it
> should have ignored it because the time windows received from logA.txt  y
> (2018-10-22 10:21:15) and k (2018-10-22 10:21:25)  :
>
> LOG file opened at 04/26/18 06:11:52
>
> ERR-2: OS message: No child processes
> operation "wait", location "skudmi:prp:6"
>
>
> In short I would like to only search in lines :
>
>  LOG file opened at 10/22/18 10:21:23
>
>
> ERR-3: patchObjectPossible causes are:"
>
> Trim whitespace same as SQL Loader
>
>
> How do I acheive this ?
>
> Please advice ,
>
> Thanks in advance,
>
>
>
> >
> > --
> > 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
> >
> >
> >
> >
> >
> >
> > ---------- Forwarded message ----------
> > From: Bob Gailer <bgailer at gmail.com>
> > To: Adam Eyring <adameyring at gmail.com>
> > Cc: benjamin.placella at maine.edu, tutor at python.org
> > Bcc:
> > Date: Fri, 26 Oct 2018 15:03:36 -0400
> > Subject: Re: [Tutor] Python Help
> > On Oct 26, 2018 1:20 PM, "Adam Eyring" <adameyring at gmail.com> wrote:
> > >
> > > Try this cleaned up version with colons in the right places, dollar
> signs
> > removed, and other corrections:
> >
> > Does it do what you want?
> >
> > > beefmeals=int(input("Enter number of beef meals: "))
> > > shitmeals=int(input("Enter number of vegan meals: "))
> > > party = beefmeals + shitmeals
> > > print("Total meals", party)
> > > a = 0
> > > b = 0
> > > c = 0
> >
> > There is no need for three variables here. You only need one to represent
> > room cost. If you make that change then you will also not need to
> > initialize the room cost variable. Makes the code simpler to maintain and
> > read and understand.
> >
> > > if party <= 50:
> > >
> > >     a=75
> > >     print("Room cost $75")
> >
> > If you use one variable for room cost then you can use just one print
> just
> > above the room tax line.
> >
> > > elif party <= 150:
> > >
> > >     b=150
> > >     print("Room cost $150")
> > > else:
> > >     c=250
> > >     print("Room cost $250")
> > > roomtax = party * 0.065
> > > print("Room tx", roomtax)
> > > print("Beef Meals", beefmeals)
> > > beef = (beefmeals * 15.95)
> > > print("Beef cost", beef)
> > > print("Vegan Meals", shitmeals)
> > > shit = (shitmeals * 10.95)
> > > print("Vegan cost", shit)
> > > cost=(beef + shit)
> > > grat= cost * 0.18
> > > print("Gratuity", grat)
> > > GT = print("Grand total", grat + beef + shit + a + b + c)
> >
> > The print function always returns None. Therefore the effect of this
> > statement is to assign None to GT. Also note that you don't use GT later
> > on.
> >
> >
> >
> >
> > ---------- Forwarded message ----------
> > From: Adam Eyring <adameyring at gmail.com>
> > To: bgailer at gmail.com
> > Cc: benjamin.placella at maine.edu, tutor at python.org
> > Bcc:
> > Date: Fri, 26 Oct 2018 13:20:15 -0400
> > Subject: Re: [Tutor] Python Help
> > Try this cleaned up version with colons in the right places, dollar signs
> > removed, and other corrections:
> >
> > beefmeals=int(input("Enter number of beef meals: "))
> > shitmeals=int(input("Enter number of vegan meals: "))
> > party = beefmeals + shitmeals
> > print("Total meals", party)
> > a = 0
> > b = 0
> > c = 0
> > if party <= 50:
> >     a=75
> >     print("Room cost $75")
> > elif party <= 150:
> >     b=150
> >     print("Room cost $150")
> > else:
> >     c=250
> >     print("Room cost $250")
> > roomtax = party * 0.065
> > print("Room tx", roomtax)
> > print("Beef Meals", beefmeals)
> > beef = (beefmeals * 15.95)
> > print("Beef cost", beef)
> > print("Vegan Meals", shitmeals)
> > shit = (shitmeals * 10.95)
> > print("Vegan cost", shit)
> > cost=(beef + shit)
> > grat= cost * 0.18
> > print("Gratuity", grat)
> > GT = print("Grand total", grat + beef + shit + a + b + c)
> >
> > On Fri, Oct 26, 2018 at 7:28 AM Bob Gailer <bgailer at gmail.com> wrote:
> >
> > > On Oct 26, 2018 6:11 AM, "Ben Placella" <benjamin.placella at maine.edu>
> > > wrote:
> > > >
> > > > I need to write code that runs a  cost calculating program with many
> > > > different variables and I honestly don't understand it
> > >
> > > Could you be more specific? What exactly don't you understand, or even
> > > better what do you understand?
> > >
> > > my code is:
> > >
> > > How could you have written so much code without understanding it?
> > >
> > > > beefmeals=int(input("Enter number of beef meals: "))
> > > > shitmeals=int(input("Enter number of vegan meals: "))
> > > > party=beefmeals+shitmeals
> > > > print(party)
> > > > if party<=50
> > >
> > > Something is missing from that last statement. Can you tell what it is?
> > Do
> > > you know how to find out? Hint use help.
> > >
> > > Hint 2 it is also missing from the elif and else statements.
> > >
> > > > a=75
> > > > print("Room cost $75")
> > > > elif party <=150
> > > > b=150
> > > > print("Room cost $150")
> > > > else
> > > > c=250
> > > > print("Room cost $250")
> > > > roomtax=party*0.065
> > > > print(roomtax)
> > > > print("Beef Meals", beefmeals)
> > > > $beef=(beefmeals*15.95)
> > > > print($beef)
> > > > print("Beef cost", $$beef)
> > > > print("Vegan Meals", shitmeals)
> > > > $shit=(shitmeals*10.95)
> > > > print($shit)
> > > > cost=($beef+$shit)
> > > > grat=cost*0.18)
> > > > print(grat)
> > > > GT=(grat+$beef+$shit+(a,b,c))
> > >
> > > There is a convention in Python that and all uppercase name is a
> > constant.
> > > This is not a requirement.
> > >
> > > > print(GT)
> > > >
> > > > This is what the output is supposed to be:
> > >
> > > I don't see any output here. Alan''s responses may help you figure that
> > > out.
> > > _______________________________________________
> > > Tutor maillist  -  Tutor at python.org
> > > To unsubscribe or change subscription options:
> > > https://mail.python.org/mailman/listinfo/tutor
> > >
> >
> >
> >
> >
> > ---------- Forwarded message ----------
> > From: Adam Eyring <adameyring at gmail.com>
> > To: bgailer at gmail.com
> > Cc: benjamin.placella at maine.edu, tutor at python.org
> > Bcc:
> > Date: Fri, 26 Oct 2018 16:04:01 -0400
> > Subject: Re: [Tutor] Python Help
> > On Fri, Oct 26, 2018 at 3:03 PM Bob Gailer <bgailer at gmail.com> wrote:
> >
> > > On Oct 26, 2018 1:20 PM, "Adam Eyring" <adameyring at gmail.com> wrote:
> > > >
> > > > Try this cleaned up version with colons in the right places, dollar
> > > signs removed, and other corrections:
> > >
> > > Does it do what you want?
> > >
> > > > beefmeals=int(input("Enter number of beef meals: "))
> > > > shitmeals=int(input("Enter number of vegan meals: "))
> > > > party = beefmeals + shitmeals
> > > > print("Total meals", party)
> > > > a = 0
> > > > b = 0
> > > > c = 0
> > >
> > > There is no need for three variables here. You only need one to
> represent
> > > room cost. If you make that change then you will also not need to
> > > initialize the room cost variable. Makes the code simpler to maintain
> and
> > > read and understand.
> > >
> > > > if party <= 50:
> > > >
> > > >     a=75
> > > >     print("Room cost $75")
> > >
> > > If you use one variable for room cost then you can use just one print
> > just
> > > above the room tax line.
> > >
> > > > elif party <= 150:
> > > >
> > > >     b=150
> > > >     print("Room cost $150")
> > > > else:
> > > >     c=250
> > > >     print("Room cost $250")
> > > > roomtax = party * 0.065
> > > > print("Room tx", roomtax)
> > > > print("Beef Meals", beefmeals)
> > > > beef = (beefmeals * 15.95)
> > > > print("Beef cost", beef)
> > > > print("Vegan Meals", shitmeals)
> > > > shit = (shitmeals * 10.95)
> > > > print("Vegan cost", shit)
> > > > cost=(beef + shit)
> > > > grat= cost * 0.18
> > > > print("Gratuity", grat)
> > > > GT = print("Grand total", grat + beef + shit + a + b + c)
> > >
> > > The print function always returns None. Therefore the effect of this
> > > statement is to assign None to GT. Also note that you don't use GT
> later
> > on.
> > >
> >
> > You're right - GT is not needed. The print does work with or without "GT
> > =" in Python 3.6.5, though.
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > https://mail.python.org/mailman/listinfo/tutor
> >
>
>
> --
> Asad Hasan
> +91 9582111698
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>


-- 
Asad Hasan
+91 9582111698


More information about the Tutor mailing list