I have a file as follows:
Col1 Col2
A 1
A 1
A 1
A 2
A 1
A 1
A 3
A 1
A 1
A 1
I want to read the file line by line in Python. When I reach a line with Col2 > 1, I want to skip the number of lines equal to Col2. Here, when reached to A 2
, I want to skip the next 2 lines and when I reach to Col2 equal to 3, I want to skip the next 3 lines and so on.
If I read the whole file in a list, I can do the following:
k = [1, 1, 1, 2, 1, 1, 3, 1, 1, 1]
i = []
for element in k:
if element > 1:
h = k.index(element)
i.append(h)
for j in range(1,element+1):
i.append(h+j)
new_list = []
for d in range(1, len(k)+1):
if not d in i:
new_list.append(k[d-1])
But my actual file is 7.2 GB so I thought to be more memory efficient, to read it line by line. How could I then implement this in Python?