If you want to delete extea lines:
For this purpose, you can check 2 conditions for each of them, if the line does not follow an empty empty line, or the line must precede the line corresponding to the next regular expression ^\d{2}:\d{2},\d{3}\s$.
, - temp itertools.tee next. re.match .
from itertools import tee
import re
with open('ex.txt') as f,open('new.txt','w') as out:
temp,f=tee(f)
next(temp)
try:
for line in f:
if next(temp) !='\n' or re.match(r'^\d{2}:\d{2},\d{3}\s$',pre):
out.write(line)
pre=line
except :
pass
:
1
17:02,111
Problem report related to
2
17:05,223
Restarting the systems
3
18:02,444
Must erase hard disk
:
, , , \n\n ($):
r"(.*?)(?=\n\n|$)"
, , :
ex.txt:
1
17:02,111
Problem report related to
router
another line
2
17:05,223
Restarting the systems
3
18:02,444
Must erase hard disk
now due to compromised data
line 5
line 6
line 7
:
def splitter(s):
for x in re.finditer(r"(.*?)(?=\n\n|$)", s,re.DOTALL):
g=x.group(0)
if g:
yield g
import re
with open('ex.txt') as f,open('new.txt','w') as out:
for block in splitter(f.read()):
first,second,third= re.split(r'(\d{2}:\d{2},\d{3}\n)',block)
out.write(first+second+third.replace('\n',' '))
:
1
17:02,111
Problem report related to router another line
2
17:05,223
Restarting the systems
3
18:02,444
Must erase hard disk now due to compromised data line 5 line 6 line 7
:
splitter , , .