If the numbers don't span record breaks then I think that this can be done more simply. This is your data.
1.00 3 4.3 5.6 2.3 4 12.4 0.5 10.2 1.10 8
5.9 11.2 7.3 1.20 8 0.2 1.2 4.2 11 23.1 4.0
7.3 13 4.4 1.7 0.5
Here's the code.
from decimal import Decimal
def records(currentTime=Decimal('1.00')):
first = True
with open('sample.txt') as sample:
for line in sample.readlines():
for number in line.split():
if Decimal(number) == currentTime:
if first:
first = False
else:
yield record
record = [number]
currentTime += Decimal('0.1')
else:
record.append(number)
yield record
for record in records():
print (record)
Here's the output.
['1.00', '3', '4.3', '5.6', '2.3', '4', '12.4', '0.5', '10.2']
['1.10', '8', '5.9', '11.2', '7.3']
['1.20', '8', '0.2', '1.2', '4.2', '11', '23.1', '4.0', '7.3', '13', '4.4', '1.7', '0.5']
EDIT: This version operates on the same lines but does not assume that numbers cannot span record breaks. It uses stream I/O. The main thing you would change would be the size of the gulps of data and, of course, the source.
from decimal import Decimal
from io import StringIO
sample = StringIO('''1.00 3 4.3 5.6 2.3 4 12.4 0.5 10.2 1.10 8 \n5.9 11.2 7.3 1.20 8\n.15 0.2 1.2 4.2 11 23.1 4.0 \n7.3 13 4.4 1.7 0.5''')
def records(currentTime=Decimal('1.00')):
first = True
previousChunk = ''
exhaustedInput = False
while True:
chunk = sample.read(50)
if not chunk:
exhaustedInput = True
chunk = previousChunk
else:
chunk = (previousChunk + chunk).replace('\n', '')
items = chunk.split()
for number in items[:len(items) if exhaustedInput else -1]:
if Decimal(number) == currentTime:
if first:
first = False
else:
yield record
record = [number]
currentTime += Decimal('0.1')
else:
record.append(number)
if exhaustedInput:
yield record
break
else:
previousChunk = chunk.split()[-1]
for record in records():
print (record)
Here is the output.
['1.00', '3', '4.3', '5.6', '2.3', '4', '12.4', '0.5', '10.2']
['1.10', '8', '5.9', '11.2', '7.3']
['1.20', '8.15', '0.2', '1.2', '4.2', '11', '23.1', '4.0', '7.3', '13', '4.4', '1.7', '0.5']