因此有一个单独的文件,其中包含所有CSS值,其中每个值的开始和结尾用特殊符号(例如$$$
指定,并且该值的关键字位于{ {1}}:
<>
话要说的是,我想把这整个事情做成一个字典,其关键词为$$$
<navbar> /*this is the key word */
.navbar { /*here starts the value of the keyword `navbar` until the closing sign `$$$` */
margin: 0 auto;
border-bottom: 1px solid rgba(0,0,0,0.1);
}
.navbar nav ul {
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
$$$ /* here it ends */
$$$ /* another value starts */
<burger> /*this is the key word */
.burger input + label { /*here starts the value of the keyword `navbar` until the closing sign `$$$` */
position: fixed;
top: 20px;
right: 40px;
height: 20px;
width: 15px;
z-index: 5;
}
$$$ /* here it ends */
and so on
内的单词,而键的值为<>
内的单词后面的块直到结束<>
因此在python文件中,输出应为:
$$$
我该如何实施?
答案 0 :(得分:1)
正则表达式可以解救!
import re
import json
data = """
$$$
<navbar>
.navbar {
margin: 0 auto;
border-bottom: 1px solid rgba(0,0,0,0.1);
}
.navbar nav ul {
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
$$$
$$$
<burger>
.burger input + label {
position: fixed;
top: 20px;
right: 40px;
height: 20px;
width: 15px;
z-index: 5;
}
$$$
"""
pat = re.compile(r'\$\$\$.*?<(.*?)>(.*?)\$\$\$', re.S)
def main():
result = {}
buf = data
while True:
m = pat.search(buf)
if not m:
break
result[m.group(1)] = m.group(2)
buf = buf[m.end():]
for r in result:
print r
print result[r]
main()
输出:
navbar
.navbar {
margin: 0 auto;
border-bottom: 1px solid rgba(0,0,0,0.1);
}
.navbar nav ul {
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
burger
.burger input + label {
position: fixed;
top: 20px;
right: 40px;
height: 20px;
width: 15px;
z-index: 5;
}