title: How do I modify only 2 very specific lines of an HTML code using python?
body: <p>I have a project where I detect if the parking slot is full or not using image processing in OpenCV and i need to display the results on a HTML page accordingly.</p>
  
  <p>Say I have two parking slots, and one of them is occupied, my code shows the appropriate answer, and i even have a basic HTML page ready. The HTML code is:</p>
  
  <pre><code>&lt;style&gt;
  .rect {
      height: 100px;
      width: 50px;
      display: inline-block;
      background-color: green;
  }
  .alert {
      background-color: green;
  }
  &lt;/style&gt;
  &lt;head&gt;
        &lt;meta http-equiv="refresh" content="01"&gt;
      &lt;title&gt;
          Slot Data
      &lt;/title&gt;
  &lt;/head&gt;          
  &lt;body&gt; 
    &lt;div class="rect"&gt;&lt;/div&gt;
  
    &lt;div class="rect alert"&gt;&lt;/div&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>So in my python code, I used string replace to find and replace the word green with red(if occupied) and find and replace red with green(if empty) in the HTML code.</p>
  
  <p>Works well for one slot. If i have 2 slots, the color change is reflected on box boxes even if only one slot is full, that's because it will replace all instances of 'green' with 'red' and vice-versa</p>
  
  <p>What I want is to modify just line no. 6 and line no. 9 of my HTML file.
  Is there a way to modify just line no. 6 for slot 1 and line no. 9 for slot 2?</p>
  
  <p>Here is my python code snippet:</p>
  
  <pre><code>if count1 &gt;= 0.65 * 122*85:
                  print "car0 absent"
                  cv2.rectangle(dst1,(8,8),(340,488),(0,255,0),2) #green
                  cv2.putText(dst1,'slot empty',(12,450), font, 1,(255,255,255),1,cv2.LINE_AA)
                  f = open('test.html','r')
                  filedata = f.read()
                  f.close()
                  newdata = filedata.replace("red","green")
                  f = open('test.html','w')
                  f.write(newdata)
                  f.close()
  
  
              else:
                  print "car0 present"
                  cv2.rectangle(dst1,(8,8),(340,488),(0,0,255),2) #red
                  cv2.putText(dst1,'slot occupied',(12,450), font, 1,(255,255,255),1,cv2.LINE_AA)
                  f = open('test.html','r')
                  filedata = f.read()
                  f.close()
                  newdata = filedata.replace("green","red")
                  f = open('test.html','w')
                  f.write(newdata)
                  f.close()
  </code></pre>
  
  <p>The above snipped is for slot 1. The same code repeats for slot 2 as well. So I only need to change '6' to '9' for slot 2.</p>
  
  <p>is there anything like </p>
  
  <pre><code>f.linenumber(6) = '__'
  f.linenumber(9) = '__'
  </code></pre>
  
  <p>that?</p>
  
  <p>I tried many combinations of file i/o commands but none work!</p>