Combine every two lines while reading .txt file in python
<p>I'm currently working with very large files in Python that look like</p>

<pre><code>junk
junk
junk
--- intermediate:
1489       pi0     111 [686] (1491,1492)   
                             0.534    -0.050    -0.468     0.724     0.135
1499       pi0     111 [690] (1501,1502)   
                            -1.131     0.503    12.751    12.812     0.135
--- final:
 32        e-      11 [7]    
                             9.072    20.492   499.225   499.727     0.001
 33        e+     -11 [6]    
                           -11.317   -17.699  2632.568  2632.652     0.001
 12         s       3 [10] (91)  &gt;43 {+5}
                             2.946     0.315    94.111    94.159     0.500
 14         g      21 [11] (60,61)  34&gt;&gt;16 {+7,-6}
                            -0.728     3.329     5.932     6.907     0.950
------------------------------------------------------------------------------
junk
junk
--- intermediate:
repeat
</code></pre>

<p>I want to combine every two lines after the "---final" line until the "----------------" line. For example, I'd like an output file to read</p>

<pre><code> 32        e-      11 [7]      9.072    20.492   499.225   499.727     0.001
 33        e+     -11 [6]    -11.317   -17.699  2632.568  2632.652     0.001
 12         s       3 [10]     2.946     0.315    94.111    94.159     0.500
 14         g      21 [11]    -0.728     3.329     5.932     6.907     0.950
</code></pre>

<p>Notice how I'm omitting the extra entries in the lines without spaces. My current approach is</p>

<pre><code>start = False
for line in myfile:
    line = line.strip()
    fields = line.split()
    if len(fields)==0:
        continue
    if not start:
        if fields[0] == "----final:":
            start = True
        continue
</code></pre>

<p>len(fields)==0 should end the script at the "---------" line and continue until it sees another "----final" line. What I currently don't know how do is combine the two lines together while omitting the extra information in the lines without spaces. Any suggestions? </p>

<p>A quick and dirty way of merging every other line:</p>

<pre><code>for i in range(0,len(lines),2):

    fields1 = lines[i].strip().split()
    fields2 = lines[i+1].strip().split()
    print("\t".join(fields1[:4]+fields2))
</code></pre>

<p>Note that I considered here that all the lines to be merged are extracted and put in a list called <code>lines</code> and that I simply hard coded the number (4) of elements that will be kept from every first line.</p>

python