title: How to replace a value in line of text file python
body: <p>I have a stock file like below.</p>
  <blockquote>
  <p>Mo, M, 15,</p>
  <p>Jen, F, 14</p>
  </blockquote>
  <p>I need to replace a variable in this text file so my code, as below, finds the line that I need to change, then puts these line values into a list. I try to use replace but it doesn't work and for some reason my whole file deletes.</p>
  <p>How do I place the value for example change Mo's age to 20?</p>
  <p>My code is something like below:</p>
  <pre><code>newAge = &quot;20&quot;
  
  with open(&quot;file.txt&quot;) as f:
      for line in f:
         if &quot;Mo&quot; in line:
             list = line.split()
             replace = list.replace(list[2]&quot;&quot;this is value of age 15&quot;&quot;, newAge) 
  </code></pre>
  <p>Any help is appreciated thanks.</p>