0

我想在python中找到导入的CSS文件中出现的宽度数。

我正在使用 file() 函数

    css = open("/Users/john/Work/html/Ribbon/header.css")  
    print(css.read())
    #output

#back {
    width: 680px;
    height: 280px;
    border: 2px solid #000;
    margin: 100px auto;
    background: url("background.png");
    position: relative;
}

#back ul {
    margin-top: 115px;
    margin-left: 80px;
    width: 476px;
    height: 39px;
    border: 2px solid #b4c5cf;
    background: #f3f8fa;
}

#back #topborder {
    background: url("top_border.png");
    position: absolute;
    width: 100%;
    height: 23px;

}

#back #bottomborder {
    background: url("bottom_border.png");
    width: 100%;
    height: 23px;
    bottom: 0;
    position: absolute;
}

我是python新手,请提出更多方法。请帮助我,如何进一步进行。

谢谢。

4

2 回答 2

0

有各种各样的 CSS 解析器。向 Google 询问“python css 解析器”。使用其中之一,我确信查找/计算相关属性会很容易。

于 2012-05-14T07:18:15.577 回答
0

我不是 100% 确定你在问什么,但如果你想计算width:CSS 文件中定义的数量,那么你可以使用以下(非常简单的)方法:

count = 0
with open('/Users/john/Work/html/Ribbon/header.css', 'r') as f:
    for line in f:
        if "width:" in line:
            count+=1
print("Found %d instances of 'width:'." % count)
于 2012-05-15T18:02:55.560 回答