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>as long as you know the exact lines that surround the section you want:</p>

<pre><code>#split the large text into lines
lines = large_text.split('\n')
#get the indexes of the beginning and end of your target section
idx_start = lines.index("--- final:")
idx_finish= lines.index("------------------------------------------------------------------------------")
#iterate through the section in steps of 2, split on spaces, remove empty strings, print them as tab delimited
for idx in range( idx_start+1, idx_finish, 2):
    out = list(filter(None,(lines[idx]+lines[idx+1]).split(" ")))
    print("\t".join(out))
</code></pre>

<p>Where <code>large_text</code> is the file imported as a giant string.</p>

<p><strong>EDIT</strong>
In order to open the file `large_text.txt' as a string try this:</p>

<pre><code>with open('large_text.txt','r') as f:
    #split the large text into lines
    lines = f.readlines()
    #get the indexes of the beginning and end of your target section
    idx_start = lines.index("--- final:")
    idx_finish= lines.index("------------------------------------------------------------------------------")
    #iterate through the section in steps of 2, split on spaces, remove empty strings, print them as tab delimited
    for idx in range( idx_start+1, idx_finish, 2):
        out = list(filter(None,(lines[idx]+lines[idx+1]).split(" ")))
        print("\t".join(out))
</code></pre>

<p><strong>Assumptions</strong></p>

<ol>
<li>You know the lines that seperate the section of interest (IE: "--- final:" )</li>
<li>Your values are space not tab delimited. If not change <code>split(" ")</code> to <code>split("\t")</code> </li>
</ol>

<p><strong>Should be the winner</strong>
Added that formatting fix to the one set of lines. Same assumptions hold true.</p>

<pre><code>with open('./large_text.txt','r') as f:
    #split the large text into lines
    lines = f.read().split("\n")
    #get the indexes of the beginning and end of your target section
    idx_start = lines.index("--- final:")
    idx_finish= lines.index("------------------------------------------------------------------------------")
    #iterate through the section in steps of 2, split on spaces, remove empty strings, print them as tab delimited
    for idx in range( idx_start+1, idx_finish, 2):
        line_spaces = list(filter(None,lines[idx].split(" ")))[0:4]
        other_line = list(filter(None,(lines[idx+1]).split(" ")))
        out = line_spaces + other_line
        print("\t".join(out))
</code></pre>

python