Question: How to read two non-consecutive lines in a file once in python
0
gravatar for jing.mengrabbit
2.5 years ago by
jing.mengrabbit40 wrote:

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!

sequencing genome • 3.1k views
ADD COMMENTlink modified 2.5 years ago by mohammadhassanj110 • written 2.5 years ago by jing.mengrabbit40

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 REPLYlink written 2.5 years ago by skbrimer640
0
gravatar for skbrimer
2.5 years ago by
skbrimer640
United States
skbrimer640 wrote:

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 COMMENTlink written 2.5 years ago by skbrimer640

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

ADD REPLYlink written 2.5 years ago by jing.mengrabbit40
0
gravatar for mohammadhassanj
2.5 years ago by
mohammadhassanj110 wrote:

"""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 COMMENTlink written 2.5 years ago by mohammadhassanj110

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

ADD REPLYlink written 2.5 years ago by jing.mengrabbit40
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1058 users visited in the last hour