title: Replace specific string in file in Python
body: <p>To start off, because I've been burned before by someone with a power trip, this question is not for homework.</p>
  
  <p>Anyway, I have a text file that is something like the following:</p>
  
  <pre><code>####
  # File section 1
  ####
  
  1.0   abc   Description1
  6.5   def   Description2
  1.0 2.0 3.0   ghi   Description3
  11    jkl   Description
  
  ####
  # File section 2
  ####
  
  1.0   abc   Description1
  12.5   def   Description2
  1.0 2.0 3.0   ghi   Description3
  11    jkl   Description
  
  #### End file
  </code></pre>
  
  <p>I would like to replace the string "1.0" in the 2 lines:</p>
  
  <pre><code>1.0   abc   Description1
  </code></pre>
  
  <p>However, NOT the "1.0" string in the lines:</p>
  
  <pre><code>1.0 2.0 3.0   ghi   Description3
  </code></pre>
  
  <p>The current code that I'm using is:</p>
  
  <pre><code>with open('sample_file.txt','r') as file:
      filedata = file.read()
      filedata = filedata.replace('1.0','2.0')
  with open('sample_file.txt','w') as file:
      file.write(filedata)
  </code></pre>
  
  <p>However the result is that all occurrences of "1.0" get replaced. Then I have to go back into the file, and correct the bug. The resultant file that I would like to get is:</p>
  
  <pre><code>####
  # File section 1
  ####
  
  2.0   abc   Description1
  6.5   def   Description2
  1.0 2.0 3.0   ghi   Description3
  11    jkl   Description
  
  ####
  # File section 2
  ####
  
  2.0   abc   Description1
  12.5   def   Description2
  1.0 2.0 3.0   ghi   Description3
  11    jkl   Description
  
  #### End file
  </code></pre>
  
  <p>How can I get that? I couldn't find an example solution to this type of issue. Thank you all for your help.</p>
  
  <p>EDIT: My fault for not clarifying, but the string I want to replace isn't always "1.0", nor always 3 characters long. It could be "-12.3", for example. I would like to make the code as generic as possible.</p>
  
  <p>I also tried using rsplit to isolate the first string using space as a delimiter, but that doesn't seem to work for file writing.</p>
  
  <p>========================</p>
  
  <p>EDIT2: I found a way to do this, though it seems to be quite a round-about method:</p>
  
  <pre><code>with open('sample_file.txt','r') as file:
      filedata = file.readlines()
          for line in filedata:
              if 'abc' in line:
                  oriline = line
                  newline = line.replace(str(spk),str(newspk))
  with open('sample_file.txt','r') as file:
      filedata = file.read()
      filedata = filedata.replace(str(oriline),str(newline))
  with open('sample_file.txt','w') as file:
      file.write(filedata)
  </code></pre>
  
  <p>Basically, it would open the file, read line by line for the whole line that contains the specific string I want, then just store it into memory. Then open the file again, read everything, and just replace that whole string. Then open the file, and write the file.</p>
  
  <p>It does what I want, but is there a way to simplify the code?</p>