title: Python: replace line in file starting with a pattern
body: <p>In a text file there is this line</p>
  
  <pre><code>initial_mass = unknown_int 
  </code></pre>
  
  <p>I want to replace the line with </p>
  
  <pre><code>initial_mass = known_int 
  </code></pre>
  
  <p>As the names suggest, I won't know the initial value but I want to replace whatever it is with a known value. Using Python, how do I search for a line that starts with 'initial_mass', then replace the entire line with 'initial_mass = known_int'?</p>
  
  <p>I am thinking along the lines of</p>
  
  <pre><code>import fileinput
      for line in fileinput.FileInput(filename, inplace=True):
      line=line.replace(old, new)
      print(line),  
  </code></pre>
  
  <p>How can I set <code>old</code> to the line containing <code>initial_mass = uknown_int</code>?
  I have looked at <code>startswith()</code> but I don't know how to use that to get what I want. I've also tried regex and find it utterly confusing.</p>
  
  <p>The file is not particularly large, but I will need to iterate over this process many times. </p>