Extracting information from a large specific header formatted file
<p>I am new to python. I have a large header formatted input file where header line starts with '>'. My file is like :</p>

<pre><code>&gt;NC_23689
#
# XYZ
# Copyright (c)  BLASC
#
# Predicted binding regions
#   No.                Start         End      Length
#   1                      1          25          25
#   2                     39          47           9
#
&gt;68469409
#
# XYZ
# Copyright (c)  BLASC
#
# Predicted binding regions
#   None.
#
# Prediction profile output:
#   Columns:
#   1 - Amino acid number
#   2 - One letter code
#   3 -  probability value
#   4 - output
#
1   M     0.1325        0
2   S     0.1341        0
3   S     0.1384        0
&gt;68464675
#
# XYZ
# Copyright (c)  BLASC
#
# Predicted binding regions
#   No.                Start         End      Length
#   1                     13          24          12
#   2                     31          53          23
#   3                     81          95          15
#   4                    115         164          50
#
...
...
</code></pre>

<p>I want to extract each  header and its corresponding Start-End value(s) (after  Predicted binding regions line) in a (output.txt file). For the above (input.txt), the output will be: </p>

<pre><code>NC_23689: 1-25, 39-47
68464675: 13-24, 31-53, 81-95, 115-164
</code></pre>

<p>I have tried :</p>

<pre><code>with open('input.txt') as infile, open('output.txt', 'w') as outfile:
   copy = False
   for line in infile:
        if line.strip() == "&gt;+":
            copy = True
    elif line.strip() == "#   No.                Start         End      Length":
            copy = True
        elif line.strip() == "#":
            copy = False
        elif copy:
            outfile.write(line)
</code></pre>

<p>But it gives me:</p>

<pre><code>#   1                      1          25          25
#   2                     39          47           9
#   1                     13          24          12
#   2                     31          53          23
#   3                     81          95          15
#   4                    115         164          50
</code></pre>

<p>Which is obviously not right. I get the range but without header descriptors and with some extra values. How can I get my above mentioned output?
Thanks</p>

<p>Ps. I am using python 2.7 in my Windows7 machine.</p>

<p>Try this:</p>

<pre><code>with open("file.txt") as f:
    first_time = True
    for line in f:
        line = line.rstrip()
        if line.startswith("&gt;"):
            if not first_time:
                if start_ends:
                    print("{}: {}".format(header,", ".join(start_ends)))        
            else:
                first_time = False    
            header = line.lstrip("&gt;")
            start_ends = []
        elif len(line.split()) == 5 and "".join(line.split()[1:]).isnumeric():
            start_ends.append("{}-{}".format(line.split()[2],line.split()[3]))
    if start_ends:
        print("{}: {}".format(header,", ".join(start_ends))) 

# Outputs:
# NC_23689: 1-25, 39-47
# 68464675: 13-24, 31-53, 81-95, 115-164
</code></pre>

python