Python Forum
First line with digits before last line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First line with digits before last line
#1
Greetings!
I’m parsing log files, 95% of the files have the lines I’m looking for.
5% does not, and I must get the first line (Line starts with date time “2022-08-14 14:37:46 ”)
And the last line (Line starts with date time “2022-08-14 14:39:00”)
The problem is not all last lines have a Date Time in the string.
I need to read the lines before the last line until I find one that starts with the data time ...

Here is what I got so far, it does not work as I wanted Confused :
import re

with open(r"C:/01/last_line.txt") as mfiler:
    frt_ln = mfiler.readline()
    print(f" Fl -> {frt_ln}")
    
    for rn_l in mfiler: 
        if 'Start' in rn_l :
            continue        
            # do something with the lines
        last_line = rn_l
    print(f" Last Line ->{last_line}")
    if not re.search('^\d+', last_line) :
        next
    else :
        print(f" Line with the DateTime -> {last_line}")
        #break
Here is a short example of the file:

2022-08-14 14:37:46.523,17784 ,Information,"==================== Bac Start Run ===================="
2022-08-14 14:37:46.523,17784 ,Information,"Bac Info:
[DS_DK] Bac Test Result : Passed
[DS_DK] Bac Iteration Result : Passed
2022-08-14 14:37:46.524,17784 ,Warning
Condition: NO Condition
Allowed Stages: Any Stage
Set Type: Hard
2022-08-14 14:39:00.032,15060 ,Information, Available network interfaces :
[Bac Setup] Ethernet Connection -2
[Bac Setup] USB 3.0 to GB Ethernet

Could you help me with this?
Thank you.
Reply


Messages In This Thread
First line with digits before last line - by tester_V - Aug-21-2022, 11:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Line graph with two superimposed lines sawtooth500 4 381 Apr-02-2024, 08:56 PM
Last Post: sawtooth500
  How to add multi-line comment section? Winfried 1 240 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  break print_format lengthy line akbarza 4 416 Mar-13-2024, 08:35 AM
Last Post: akbarza
  Reading and storing a line of output from pexpect child eagerissac 1 4,309 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  coma separator is printed on a new line for some reason tester_V 4 536 Feb-02-2024, 06:06 PM
Last Post: tester_V
  problem with spliting line in print akbarza 3 428 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Unable to understand the meaning of the line of code. jahuja73 0 328 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Receive Input on Same Line? johnywhy 8 771 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  Reading in of line not working? garynewport 2 882 Sep-19-2023, 02:22 PM
Last Post: snippsat
  'answers 2' is not defined on line 27 0814uu 4 758 Sep-02-2023, 11:02 PM
Last Post: 0814uu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020