简体   繁体   中英

How do I modify only 2 very specific lines of an HTML code using python?

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.

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:

<style>
.rect {
    height: 100px;
    width: 50px;
    display: inline-block;
    background-color: green;
}
.alert {
    background-color: green;
}
</style>
<head>
      <meta http-equiv="refresh" content="01">
    <title>
        Slot Data
    </title>
</head>          
<body> 
  <div class="rect"></div>

  <div class="rect alert"></div>

</body>

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.

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

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?

Here is my python code snippet:

if count1 >= 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()

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.

is there anything like

f.linenumber(6) = '__'
f.linenumber(9) = '__'

that?

I tried many combinations of file i/o commands but none work!

Here's one way to do it:

>>> lines = filedata.split('\n')
>>> lines[5] = lines[5].replace('green', 'red')
>>> lines[8] = lines[8].replace('green', 'red')
>>> newdata = '\n'.join(lines)
>>> print(newdata)
<style>
.rect {
    height: 100px;
    width: 50px;
    display: inline-block;
    background-color: red;
}
.alert {
    background-color: red;
}
</style>
<head>
      <meta http-equiv="refresh" content="01">
    <title>
        Slot Data
    </title>
</head>
<body>
  <div class="rect"></div>

  <div class="rect alert"></div>

</body>
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM