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
#2
import io
import re

data = io.StringIO("""
This is not the first line
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
""")

date_pattern = re.compile("^\d{4}-\d{2}-\d{2}")

first = last = None
for line in data:
    if re.search(date_pattern, line):
        last = line
        if first is None:
            first = line

print(first)
print(last)
Output:
2022-08-14 14:37:46.523,17784 ,Information,"==================== Bac Start Run ====================" 2022-08-14 14:39:00.032,15060 ,Information, Available network interfaces :
tester_V likes this post
Reply


Messages In This Thread
RE: First line with digits before last line - by deanhystad - Aug-22-2022, 02:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing the code line number arbiel 2 199 Jun-15-2024, 07:37 PM
Last Post: arbiel
  How to add multi-line comment section? Winfried 3 438 Jun-04-2024, 07:24 AM
Last Post: Gribouillis
Information Is it possible to multi line a Basic Function Construct line statement? If so how? BrandonKastning 7 447 May-23-2024, 03:02 PM
Last Post: deanhystad
  Line graph with two superimposed lines sawtooth500 4 501 Apr-02-2024, 08:56 PM
Last Post: sawtooth500
  break print_format lengthy line akbarza 4 522 Mar-13-2024, 08:35 AM
Last Post: akbarza
  Reading and storing a line of output from pexpect child eagerissac 1 4,511 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  coma separator is printed on a new line for some reason tester_V 4 653 Feb-02-2024, 06:06 PM
Last Post: tester_V
  problem with spliting line in print akbarza 3 529 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Unable to understand the meaning of the line of code. jahuja73 0 428 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Receive Input on Same Line? johnywhy 8 1,042 Jan-16-2024, 03:45 AM
Last Post: johnywhy

Forum Jump:

User Panel Messages

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