title: Find line, then get the next line
body: <p>I have the following problem:
  I open a file and read it line by line searching for a specific pattern. When I found it, I would like to write the entire line AND THE NEXT TWO LINES into a new file. The problem is that I don't know how to get from the line that I've found to the next 2.</p>
  
  <pre><code>AAA
  XXX
  XXX
  BBB
  XXX
  XXX
  CCC
  XXX
  XXX
  </code></pre>
  
  <p>In this example it would be that I find "BBB" and I want to get the next two lines.</p>
  
  <p>What could I do? Thank you very much for your kind help!</p>
  
  <p>Edit: I realized that I have to ask more precisely.</p>
  
  <p>This is the code from my colleague</p>
  
  <pre><code>for k in range(0,len(watcrd)):
      if cvt[k]&gt;cvmin:
          intwat+=1
          sumcv+=cvt[k]
          sumtrj+=trj[k]/((i+1)*sep/100)
          endline='%5.2f %5.2f' % (cvt[k],trj[k]/((i+1)*sep/100)) # ivan
          ftrj.write(watline[k][:55]+endline+'\n')
          fall.write(watline[k][:55]+endline+'\n')
  </code></pre>
  
  <p>For every <code>k in range</code> I would like to write <code>k, k+1, k+2</code> to the file ftrj.
  Which is the best way to do this?</p>
  
  <p>Edit 2: I am sorry, but I realized that I've made a mistake. What you suggested worked, but I realized that I have to include it in a different part of the code.</p>
  
  <pre><code>for line in lines[model[i]:model[i+1]]:
      if line.startswith('ATOM'):
      resi=line[22:26]
      resn=line[17:20]
      atn=line[12:16]
      crd=[float(line[31:38]),float(line[38:46]),float(line[46:54])]
  
      if (resn in noprot)==False and atn.strip().startswith('CA')==True:
          protcrd.append(crd)
      if (resn in reswat)==True and (atn.strip() in atwat)==True:
          watcrd.append(crd)
          watline.append(line)
  </code></pre>
  
  <p>I would think of something like this:</p>
  
  <pre><code>    (...)
      if (resn in reswat)==True and (atn.strip() in atwat)==True:
          watcrd.append(crd)
          watline.append(line)
          for i in range(1, 3):
              try:
                  watcrd.append(crd[line + i])
                  watline.append(line[line + i])
              except IndexError:
                  break
  </code></pre>
  
  <p>But it doesn't work. How can I indicate the part and the line that I want to append to this list?</p>