Q:

Python: How to extract DNA sequence based on a text file with binary content?

For example I have a fasta file with the following
sequences:
>human1
AGGGCGSTGC
>human2
GCTTGCGCTAG
>human3
TTCGCTAG

How to use python to read a text file with the following content to extract the
sequences? 1 represents true and 0 represents false. Only sequence with value 1
will be extracted.
Example text file:
0
1
1

Expected output:
>human2
GCTTGCGCTAG
>human3
TTCGCTAG

A:

for this is better to use biopython
from Bio import SeqIO

mask = ["1"==_.strip() for _ in open("mask.txt")]
seqs = [seq for seq in SeqIO.parse(open("input.fasta"), "fasta")]
seqs_filter = [seq for flag, seq in zip(mask, seqs) if flag]
for seq in seqs_filter:
  print seq.format("fasta")

you get:

>human2
GCTTGCGCTAG
>human3
TTCGCTAG

explanation
parse fasta: the format fasta may to have several lines of sequences (check fasta format), is better to use a specialized library to read (parser) and write the output
mask: I read de mask file and cast to boolean [False, True, True]
filter: use zip function for each sequence match with his mask, and following i use list Comprehensions to filter

A:

I think this may help you and I really think you should take some time learn Python. Python is a good language for bioinformatics. 
display = []
with open('test.txt') as f:
    for line in f.readlines():
        display.append(int(line.strip()))

output_DNA = []
with open('XX.fasta') as f:
    index = -1
    for line in f.readlines():
        if line[0] == '>':
            index = index + 1

        if display[index]:
            output_DNA.append(line)

print output_DNA

