title: Find and copy a line using regex in Python
body: <p>I am new to this forum and to programming and apologize in advance if I violate any of the forum rules. I have researched this extensively, but I couldn't find a solution for my problem.</p>
  
  <p>So I have a very long file that has this general structure:</p>
  
  <pre><code>data="""
     20.020001    563410   9
     20.520001    577410  20
     21.022001    591466   9
     21.522001    605466 120
     23.196001    652338   2
     25.278001    710634   7
     25.780001    724690 144
     26.280001    738690   9
     26.782001    752746  40
     27.282001    766746   9
     27.784001    780802 140
     29.372001    825266   2
     31.458001    883674   7
     31.958002    897674   8
     32.458002    911674   9
     32.958002    925674  10
  
  """
  </code></pre>
  
  <p>I imported the file using</p>
  
  <pre><code>with open("C:\blablabla\text.txt", 'r+') as infile:
  data = infile.read()
  </code></pre>
  
  <p>Now I am trying to use a regular expression to find all lines that end with 140 through 146, so I did this:</p>
  
  <pre><code>items=re.findall('.......................14[0-6]\n',data,re.MULTILINE)
  for x in items:
      print x
  </code></pre>
  
  <p>This works, but when I now try to copy those lines that contain the regular expression,</p>
  
  <pre><code>for x in items:
      if items in data:
          data.write(items)
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>if items in data:
  TypeError: 'in &lt;string&gt;' requires string as left operand, not list
  </code></pre>
  
  <p>I understand what the problem is, but I don't know how to solve it. How can I feed the left operand a string when the outcome of my regex is a list?</p>
  
  <p>Any help is much appreciated!</p>