I have a huge data file with a specific string being repeated after a defined number of lines.
counting jump between first two \'Rank\' occurrences. For example the file
I assume you want to find the number of lines in a block where each block starts with a line that contains 'Rank' e.g., there are 3 blocks in your sample: 1st has 4 lines, 2nd has 4 lines, 3rd has 1 line:
from itertools import groupby
def block_start(line, start=[None]):
if 'Rank' in line:
start[0] = not start[0]
return start[0]
with open(filename) as file:
block_sizes = [sum(1 for line in block) # find number of lines in a block
for _, block in groupby(file, key=block_start)] # group
print(block_sizes)
# -> [4, 4, 1]
If all blocks have the same number of lines or you just want to find number of lines in the first block that starts with 'Rank'
:
count = None
with open(filename) as file:
for line in file:
if 'Rank' in line:
if count is None: # found the start of the 1st block
count = 1
else: # found the start of the 2nd block
break
elif count is not None: # inside the 1st block
count += 1
print(count) # -> 4