I am processing a text file with an irregular structure that consists of a header and of data in different sections. What I aim to do is walk through a list and jump to the next section once a certain character is encountered. I made a simple example below. What is the elegant way of dealing with this problem?
lines = ['a','b','c','$', 1, 2, 3]
for line in lines:
if line == '$':
print("FOUND END OF HEADER")
break
else:
print("Reading letters")
# Here, I start again, but I would like to continue with the actual
# state of the iterator, in order to only read the remaining elements.
for line in lines:
print("Reading numbers")
You actually can have one iterator for both loops by creating your line iterator outside the for loop with the builtin function iter. This way it will be partially exhausted in the first loop and reusable in the next loop.
lines = ['a','b','c','$', 1, 2, 3]
iter_lines = iter(lines) # This creates and iterator on lines
for line in iter_lines :
if line == '$':
print("FOUND END OF HEADER")
break
else:
print("Reading letters")
for line in iter_lines:
print("Reading numbers")
The above prints this result.
Reading letters
Reading letters
Reading letters
FOUND END OF HEADER
Reading numbers
Reading numbers
Reading numbers
You could use enumerate to keep track of where you are in the iteration:
lines = ['a','b','c','$', 1, 2, 3]
for i, line in enumerate(lines):
if line == '$':
print("FOUND END OF HEADER")
break
else:
print("Reading letters")
print(lines[i+1:]) #prints [1,2,3]
But, unless you actually need to process the header portion, the idea of @EdChum to simply use index is probably better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With