Collect lines in nested loops until the end-of-record marker, and write out the resulting list to a CSV file:
import csv
with open(inputfilename) as infh, open(outputfilename, 'w', newline='') as outfh:
writer = csv.writer(outfh)
for line in infh:
if not line.startswith('+++++'):
continue
# found start, collect lines until end-of-record
row = []
for line in infh:
if line.startswith('<<<<<'):
# found end, end this inner loop
break
row.append(line.rstrip('\n'))
if row:
# lines for this record are added to the CSV file as a single row
writer.writerow(row)
The outer loop takes lines from the input file, but skips anything that doesn't look like the start of a record. Once a start is found, a second, inner loop draws more lines from the file object, and as long as they do not look like the end of the record, adds them to a list object (sans the line separator).
When the end of a record is found, the inner loop is ended, and if any lines were collected in the row list, it is written out to the CSV file.
Demo:
>>> import csv
>>> from io import StringIO
>>> import sys
>>> demo = StringIO('''\
... +++++
... line1
... line2
... <<<<<
... +++++
... rline1
... rline2
... <<<<<
... ''')
>>> writer = csv.writer(sys.stdout)
>>> for line in demo:
... if not line.startswith('+++++'):
... continue
... row = []
... for line in demo:
... if line.startswith('<<<<<'):
... break
... row.append(line.rstrip('\n'))
... if row:
... writer.writerow(row)
...
line1,line2
13
rline1,rline2
15
The numbers after the written lines are the number of bytes written, as reported by writer.writerow().