My pileup file is too big, so the program will be crashed. But thanks for your time!
Hi, I want to read two non-consecutive lines once in a very big pileup file in python, that is, the expected result is the following(the number is the line number):
1 6 (the first time reads the 1st and 6th line)
2 7 (the second time reads the 2nd and 7th line)
3 8 (the third time reads the 3rd and 8th line)
4 9
5 10
6 11
7 12
8 13
9 14
10 15
..
..
..
..
.. (the last time reads the 6th from the last line and the last line)
The line number can be gotten by using the method enumerate(file object). I am a beginner for python, and just know how to read two consecutive lines once. Could you please share with me how to get the above expected result? Thanks for your time!
2 answers
Okay its a little hacky and assuming your file is not too large you should be able to do it this way
with open("test.txt","r") as f:
lines = f.readlines()
count = 0
for i in enumerate(lines):
print(lines[count:count+5])
count +=1
this will print it the way I think you want it to print, but the whole file is loaded into memory so I'm not sure how helpful it will be, hopefully it will not freeze your machine up.
"""if you had a small file you can use readlines method and using some variables to solve it but you have a huge file and a limited memory so below script will your problem somewhat you can improve it"""
file = open("test.cpp")
byte =0
while True:
x = 1
line = file.readline() # read first line
byte+=len(line) # store line size in bytes
while (x!=6): # go on till 6th line
next_line = file.readline() # this is 6th line
x+=1 # update x
print(line,next_line) # do something with line and next_line
file.seek(byte) # start from line 2
if line == '' or next_line == '': # todo for you
break
It works for a big file. Thanks for your help!
Log in to answer this question.
I think this should work, if I understand your question correctly. Not 100% on output you want and I'm not sure if this will print in the way tou want so you will need to play around with it. Good luck