title: Appending variable string to each line in a file with Python
body: <p>I have barely any experience with file r/w in Python, and wanted to ask what the best solution for my particular case is.</p>
  
  <p>I have a tab separated file with the following structure, where each sentence is separated by a blank line:</p>
  
  <pre><code>Roundup NN
  :   :
  Muslim  NNP
  Brotherhood NNP
  vows    VBZ
  daily   JJ
  protests    NNS
  in  IN
  Egypt   NNP
  
  Families    NNS
  with    IN
  no  DT
  information NN
  on  IN
  the DT
  whereabouts NN
  of  IN
  loved   VBN
  ones    NNS
  are VBP
  grief   JJ
  -   :
  stricken    JJ
  .   .
  
  The DT
  provincial  JJ
  departments NNS
  of  IN
  supervision NN
  and CC
  environmental   JJ
  protection  NN
  jointly RB
  announced   VBN
  on  IN
  May NNP
  9   CD
  that    IN
  the DT
  supervisory JJ
  department  NN
  will    MD
  question    VB
  and CC
  criticize   VB
  mayors  NNS
  who WP
  fail    VBP
  to  TO
  curb    VB
  pollution   NN
  .   .
  
  (...)
  </code></pre>
  
  <p>I want to append to the non-empty lines of this file, first a tab and then a given string. </p>
  
  <p>For each line, the string to append will depend on the value stored in <code>lab_pred_tags</code> in the code below. For each iteration of the <code>for</code> loop, <code>lab_pred_tags</code> has the same length as the number of lines as its corresponding sentence in the text file. i.e., in the example, the lengths of <code>lab_pred_tags</code> for the 3 <code>for</code> loop iterations are 9, 15, and 12.</p>
  
  <p>For the first <code>for</code> loop iteration, <code>lab_pred_tags</code> contains the <code>list</code>: <code>['O', 'O', 'B-ORG', 'I-ORG', 'O', 'O', 'O', 'O', 'B-GPE']</code></p>
  
  <pre><code># (...) code to calculate lab_pred
  for lab, lab_pred, length in zip(labels, labels_pred, sequence_lengths):
      lab = lab[:length]
      lab_pred = lab_pred[:length]
      # Convert lab_pred from a sequence of numbers to a sequence of strings
      lab_pred_tags = d_u.label_idxs_to_tags(lab_pred, tags)
      # Now what is the best solution to append each element of `lab_pred_tags` to each line in the file?
      # Keep in mind that I will need to skip a line everytime a new for loop iteration is started
  </code></pre>
  
  <p>For the example, the desired output file is:</p>
  
  <pre><code>Roundup NN  O
  :   :   O
  Muslim  NNP B-ORG
  Brotherhood NNP I-ORG
  vows    VBZ O
  daily   JJ  O
  protests    NNS O
  in  IN  O
  Egypt   NNP B-GPE
  
  Families    NNS O
  with    IN  O
  no  DT  O
  information NN  O
  on  IN  O
  the DT  O
  whereabouts NN  O
  of  IN  O
  loved   VBN O
  ones    NNS O
  are VBP O
  grief   JJ  O
  -   :   O
  stricken    JJ  O
  .   .   O
  
  The DT  O
  provincial  JJ  O
  departments NNS O
  of  IN  O
  supervision NN  O
  and CC  O
  environmental   JJ  O
  protection  NN  O
  jointly RB  O
  announced   VBN O
  on  IN  O
  May NNP O
  9   CD  O
  that    IN  O
  the DT  O
  supervisory JJ  O
  department  NN  O
  will    MD  O
  question    VB  O
  and CC  O
  criticize   VB  O
  mayors  NNS O
  who WP  O
  fail    VBP O
  to  TO  O
  curb    VB  O
  pollution   NN  O
  .   .   O
  </code></pre>
  
  <p>What is the best solution for this?</p>