title: Parsing string into list of outputs
body: <p>I'm working with a text file, which consists of many similar reports of the following structure:</p>
  <pre><code>['NetNGlyc-1.0 Server Output - DTU Health Tech\n',
   '     Asn-Xaa-Ser/Thr sequons in the sequence output below are highlighted in blue.\n',
   '          Asparagines predicted to be N-glycosylated are highlighted in red.\n',
   &quot;Output for 'Sequence'\n&quot;,
   'Name:  Sequence  Length:  923\n',
   'MERGLPLLCAVLALVLAPAGAFRNDKCGDTIKIESPGYLTSPGYPHSYHPSEKCEWLIQAPDPYQRIMINFNPHFDLEDR      80 \n',
   'DCKYDYVEVFDGENENGHFRGKFCGKIAPPPVVSSGPFLFIKFVSDYETHGAGFSIRYEIFKRGPECSQNYTTPSGVIKS     160 \n',
   'PGFPEKYPNSLECTYIVFVPKMSEIILEFESFDLEPDSNPPGGMFCRYDRLEIWDGFPDVGPHIGRYCGQKTPGRIRSSS     240 \n',
   'GILSMVFYTDSAIAKEGFSANYSVLQSSVSEDFKCMEALGMESGEIHSDQITASSQYSTNWSAERSRLNYPENGWTPGED     320 \n',
   'SYREWIQVDLGLLRFVTAVGTQGAISKETKKKYYVKTYKIDVSSNGEDWITIKEGNKPVLFQGNTNPTDVVVAVFPKPLI     400 \n',
   'TRFVRIKPATWETGISMRFEVYGCKITDYPCSGMLGMVSGLISDSQITSSNQGDRNWMPENIRLVTSRSGWALPPAPHSY     480 \n',
   'INEWLQIDLGEEKIVRGIIIQGGKHRENKVFMRKFKIGYSNNGSDWKMIMDDSKRKAKSFEGNNNYDTPELRTFPALSTR     560 \n',
   'FIRIYPERATHGGLGLRMELLGCEVEAPTAGPTTPNGNLVDECDDDQANCHSGTGDDFQLTGGTTVLATEKPTVIDSTIQ     640 \n',
   'SEFPTYGFNCEFGWGSHKTFCHWEHDNHVQLKWSVLTSKTGPIQDHTGDGNFIYSQADENQKGKVARLVSPVVYSQNSAH     720 \n',
   'CMTFWYHMSGSHVGTLRVKLRYQKPEEYDQLVWMAIGHQGDHWKEGRVLLHKSLKLYQVIFEGEIGKGNLGGIAVDDISI     800 \n',
   'NNHISQEDCAKPADLDKKNPEIKIDETGSTPGYEGEGEGDKNISRKPGNVLKTLDPILITIIAMSALGVLLGAVCGVVLY     880 \n',
   'CACWHNGMSERNLSALENYNFELVDGVKLKKDKLNTQSTYSEA\n',
   '................................................................................      80\n',
   '.....................................................................N..........     160\n',
   '................................................................................     240\n',
   '....................N...........................................................     320\n',
   '.................................................................N..............     400\n',
   '................................................................................     480\n',
   '................................................................................     560\n',
   '................................................................................     640\n',
   '................................................................................     720\n',
   '................................................................................     800\n',
   '................................................................................     880\n',
   '...........................................                                          960\n',
   '\n',
   '(Threshold=0.5)\n',
   '----------------------------------------------------------------------\n',
   'SeqName      Position  Potential   Jury    N-Glyc\n',
   '     agreement result\n',
   '----------------------------------------------------------------------\n',
   'Sequence     150 NYTT   0.5361     (5/9)   +     \n',
   'Sequence     261 NYSV   0.5599     (6/9)   +     \n',
   'Sequence     300 NWSA   0.4157     (6/9)   -     \n',
   'Sequence     386 NPTD   0.7736     (9/9)   +++  WARNING: PRO-X1. \n',
   'Sequence     522 NGSD   0.3918     (9/9)   --    \n',
   'Sequence     842 NISR   0.4662     (6/9)   -     \n',
   'Sequence     892 NLSA   0.4099     (6/9)   -     \n',
   '----------------------------------------------------------------------\n',
   '\n',
   '\n',
   'Graphics in PostScript\n',
   '\n',
   '\n',
   'Go back.\n']
  </code></pre>
  <p>The resulting file that I'm trying to get is a list of elements, where each element would be a string, containing only the info that I want to be left. The final list structure that I'm trying to get is something like that:</p>
  <pre><code>['Sequence     150 NYTT   0.5361     (5/9)   +     \n
   Sequence     261 NYSV   0.5599     (6/9)   +     \n
   Sequence     300 NWSA   0.4157     (6/9)   -     \n',
  
  'Sequence     150 NYTT   0.5361     (5/9)   +     \n
   Sequence     261 NYSV   0.5599     (6/9)   +     \n
   Sequence     300 NWSA   0.4157     (6/9)   -     \n
   Sequence     466 NYSV   0.6178     (7/9)   +     \n
   Sequence     300 NWSA   0.4157     (6/9)   -     \n',
  
  'Sequence     150 NYTT   0.5361     (5/9)   +     \n
   Sequence     261 NYSV   0.5599     (6/9)   +     \n
   Sequence     300 NWSA   0.4157     (6/9)   -     \n',
  ...]
  </code></pre>
  <p>I managed to separate the outputs with the following code:</p>
  <pre><code>import re
  
  with open('/path_to_text_file/file.txt', 'r') as file:
      test_output = file.readlines()
  
  test_string = ''.join(map(str, test_output))  # convert the list into string
  
  # here I decided to split the outputs by 'Go back' substring
  # 1. first split by &quot;\n\n&quot; preceeding the 'Go back' substring
  # 2. then by &quot;.\n&quot; following the 'Go back'
  # 3. then by &quot;\n&quot; left 
  
  test_string_split = ' '.join(map(str, ' '.join(map(str, test_string.split('\n\n'))).split('.\n')))
  
  
  # split into element by *'Go back'* substring
  processed_test = ''.join(test_string_split).split('Go back')
  </code></pre>
  <p>Now what I have in my hands is a list of elements, where each element comprises a single output. But I haven't managed yet to strip this outputs of all unnecessary text preserving the structure of the list, where each element came from a single report.
  I tried the following logic:</p>
  <pre><code>res = [] # create a list for the final result
  
  # split each output in the text file by '\n'
  for output in processed_test: 
      output_split = ''.join(output).split('\n')
  
      # then check each line of the output for the 'Sequence' substring
      for string in output_split:
          string_el = ''.join(string)
          if re.match(&quot;Sequence.*&quot;, string_el): 
              res.append(string_el) # save matches to the resulting list
  </code></pre>
  <p>But what I get is a list of elements, where each element comprises a separate &quot;Sequence&quot;-line:</p>
  <pre><code>['Sequence     522 NGSD   0.3918     (9/9)   --    ',
   'Sequence     842 NISR   0.4662     (6/9)   -     ',
   'Sequence     892 NLSA   0.4099     (6/9)   -     ',
   'Sequence      63 NYTV   0.7796     (9/9)   +++   ',
   'Sequence     209 NITL   0.7032     (8/9)   +     ',
   'Sequence     297 NVSI   0.6256     (8/9)   +     ',
   'Sequence     365 NLSQ   0.6403     (7/9)   +     ',
   'Sequence     522 NTSH   0.5207     (6/9)   +     ',
   'Sequence     696 NCSI   0.6619     (9/9)   ++    ',
  ...
  ...
  ...]
  
  </code></pre>
  <p>Is there a way of parsing a list inside the elements themselves so as to preserve the structure of the list? The idea is that I need to understand from which report comes the info on the sequences.</p>