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>You could solve your problem with the newer <a href="https://pypi.python.org/pypi/regex" rel="nofollow"><strong><code>regex</code></strong></a> module and some regular expressions:</p>

<pre><code>import regex as re

rx = re.compile(r'''(?V1)
        (?:^---\ final:[\n\r])|(?:\G(?!\A))
        ^(\ *\d+.+?)\ *$[\n\r]
        ^\ +(.+)$[\n\r]
        ''', re.MULTILINE | re.VERBOSE)

junky_string = your_string

matches = ["    ".join(match.groups()) 
            for match in rx.finditer(junky_string)
            if match.group(1) is not None]
print(matches)
# [' 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']
</code></pre>

<p>This looks for <code>--- final:</code> at the very beginning of the line or spaces, followed by digits <strong>after</strong> having matched <code>--- final:</code> (study the <a href="https://regex101.com/r/nY0uH4/1" rel="nofollow"><strong>explanation on regex101.com</strong></a> for more details).<br>
The matched items are joined with a tabulator afterwards.</p>

python