因此,下面的脚本在HTML文件中查找关键字,并将找到的关键字的值写入文件style.css
:
from collections import OrderedDict
keyword = {
"row": '''
.row {
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
margin-right: -15px;
margin-left: -15px;
}'''
#etc
}
with open('index.html', 'r') as file:
with open('style.css', 'a') as newfile:
lines = file.readlines()
for line in lines:
if 'class="' in line:
to_replace = line.split('"')[1].split()
to_replace = OrderedDict.fromkeys(to_replace)
for key in to_replace:
if key in keyword:
newfile.write(keyword[key])
keyword[key] = ''
每次保存
index.html
文件并用字典中的某些单词进行修改时,如何运行代码? ,还有,如何防止style.css
中的CSS命令重复?
到目前为止,我只能导入库:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
下一步做什么?