Crossing lines with Python

So, I have a file that contains this:

SequenceName 4.6e-38 810..924
SequenceName_FGS_810..924 VAWNCRQNVFWAPLFQGPYTPARYYYAPEEPKHYQEMKQCFSQTYHGMSFCDGCQIGMCH
SequenceName 1.6e-38 887..992
SequenceName_GYQ_887..992 PLFQGPYTPARYYYAPEEPKHYQEMKQCFSQTYHGMSFCDGCQIGMCH

I want my program to read only lines containing these protein sequences. So far, I got this that skips the first line and reads the second:

handle = open(filename, "r")
handle.readline()
linearr = handle.readline().split()
handle.close()

fnamealpha = fname + ".txt"
handle = open(fnamealpha, "w")
handle.write(">%s\n%s\n" % (linearr[0], linearr[1]))
handle.close()

But it only processes the first sequence, and I need to process it every line containing the sequence, so I need a loop, how can I do this? The part that saves the txt file is really important, so I need to find a way by which I can combine these two goals. My output with the above code:

>SequenceName_810..924
VAWNCRQNVFWAPLFQGPYTPARYYYAPEEPKHYQEMKQCFSQTYHGMSFCDGCQIGMCH
+3
source share
2 answers

, , - , ? - - , ? :

# context manager `with` takes care of file closing, error handling
with open(filename, 'r') as handle:
    for line in handle:
        if line.startswith('SequenceName_'):
             print line.split()
             # Write to file, etc.

, , SequenceName_###.

+11

readlines for.

with open(filename, 'r') as fh:
    for line in fh.readlines:
        # do processing here

#do processing here . ( with .)

+1

All Articles