title: Convert spaces to newlines in text file using Python
body: <p>I have a text file which looks like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>15.9 17.2 18.6 10.5
  </code></pre>
  
  <p>I want to edit this file in Python so that it looks like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>15.9
  17.2
  18.6 
  10.5
  </code></pre>
  
  <p>This means that I need to replace the space strings by newline strings and save the text.</p>
  
  <p>I tried this but it doesn't work:</p>
  
  <pre><code>f = open("testfile.txt", "w")
  
  for line in f:
      if ' ' in line:
          line2 = line.replace(' ' , '\n')
      print(line2)
  for i in line2:
      f.write(line2(i))
  f.close
  </code></pre>
  
  <p>The print for <code>line2</code> is already working, but I don't get a new text file with  spaces replaced by newlines.</p>
  
  <p>How can I fix the problem and produce the desired output?</p>