Recently I bumped into some weird behaviour from comprehension generator in python
My code:
with open('test.txt') as file:
lines = (line for line in file.read().splitlines())
for line in lines:
print(line)
for line in lines:
print(line)
However instead of two iteration over the same file content I only get one:
row1
row2
row3
...
where I would expect:
row1
row2
row3
...
row1
row2
row3
...
I could not find solution to this issue on other forums so if you know the answer I would appreciate the help :-)
EDIT: This question was posted due to my lack of understanding on how generators work. I was convinced that once I finish a loop over a generator I can do another one and another as many times as I want. I am deeply sorry for this question.