简体   繁体   English

如何使用python修改HTML代码的2条非常具体的行?

[英]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. 我有一个项目,可以在OpenCV中使用图像处理功能检测停车位是否已满,并且需要在HTML页面上相应显示结果。

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. 假设我有两个停车位,其中一个已被占用,我的代码显示了正确的答案,甚至我已经准备好了基本的HTML页面。 The HTML code is: HTML代码是:

<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. 因此,在我的python代码中,我在HTML代码中使用了字符串替换来查找和替换单词green用red(如果被占用),以及将red查找并替换为green(如果为空)。

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 如果我有2个插槽,即使只有一个插槽已满,颜色变化也会反映在盒子上,这是因为它将用“红色”替换所有“绿色”实例,反之亦然

What I want is to modify just line no. 我要修改的只是行号。 6 and line no. 6和行号。 9 of my HTML file. 我的HTML文件的9。 Is there a way to modify just line no. 有没有办法修改行号。 6 for slot 1 and line no. 6用于插槽1和线号。 9 for slot 2? 9代表插槽2?

Here is my python code snippet: 这是我的python代码段:

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. 上面的片段是针对插槽1的。相同的代码也针对插槽2重复。 So I only need to change '6' to '9' for slot 2. 因此,我只需要将插槽2的“ 6”更改为“ 9”即可。

is there anything like 有什么像

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

that? 那?

I tried many combinations of file i/o commands but none work! 我尝试了文件I / O命令的许多组合,但没有任何效果!

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>
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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