How to read two non-consecutive lines in a file once in python
2
0
Entering edit mode
3.6 years ago

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!

genome sequencing • 4.1k views
ADD COMMENT
0
Entering edit mode

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

lines = [1,6,2,7,3,8]

with open(file) as pileup:
    for i in enumerate(pileup):
        if i in lines:
            print(lines)
ADD REPLY
0
Entering edit mode
3.6 years ago
skbrimer ▴ 670

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.

ADD COMMENT
0
Entering edit mode

My pileup file is too big, so the program will be crashed. But thanks for your time!

ADD REPLY
0
Entering edit mode
3.6 years ago

"""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
ADD COMMENT
0
Entering edit mode

It works for a big file. Thanks for your help!

ADD REPLY

Login before adding your answer.

Traffic: 1806 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6