I have data in blocks with non-data lines between the blocks. This code has been working but is not robust. How do I extract blocks and skip non-data blocks without consuming a line in the index test? I'm looking for a straight python solution without loading packages.
I've searched for a relevant example and I'm happy to delete this question if the answer exists.
from __future__ import print_function
BLOCK_DATA_ROWS = 3
SKIP_ROWS = 2
block = 0
with open('array1.dat', 'rb') as f:
for i in range (2):
block += 1
for index, line in enumerate(f):
if index == BLOCK_DATA_ROWS:
break
print(block, 'index', index, 'line', line.rstrip('\r\n'))
for index, line in enumerate(f):
if index == SKIP_ROWS:
break
print(' skip index', index, 'line', line.rstrip('\r\n'))
Input
1
2
3
4
5
6
7
8
9
Output
1 index 0 line 1
1 index 1 line 2
1 index 2 line 3
skip index 0 line 5
skip index 1 line 6
2 index 0 line 8
2 index 1 line 9
Edit
I also want to use a similar iteration approach with an excel sheet:
for row in ws.iter_rows()