2014-11-05 173 views
0

我有两个输入文件:一个html和一个css。我想根据css文件的内容对html文件进行一些操作。嵌套for循环迭代停止

我的HTML是这样的:(!分别为每个跨度ID)

<html> 
<head> 
     <title></title> 
    </head> 
    <body> 
    <p class = "cl1" id = "id1"> <span id = "span1"> blabla</span> </p> 
    <p class = "cl2" id = "id2"> <span id = "span2"> blablabla</span> <span id = "span3"> qwqwqw </span> </p> 
    </body> 
    </html> 

风格跨度ID在CSS文件中定义

立足于做真正的东西(跨度删除之前他们样式)我只是想从HTML打印出ID和从每个ID对应的CSS风格descritption。

代码:

from lxml import etree 

tree = etree.parse("file.html") 

filein = "file.css" 


def f1(): 

    with open(filein, 'rU') as f: 
     for span in tree.iterfind('//span'): 
      for line in f: 
       if span and span.attrib.has_key('id'): 
        x = span.get('id') 
        if "af" not in x and x in line: 
          print x, line 
def main(): 
    f1() 

所以,有两个for循环,它遍历完美,如果分开了,但如果这个功能放在一起,第一循环之后的迭代停止:

>> span1 span`#span1 { font-weight: bold; font-size: 11.0pt; font-style: normal; letter-spacing: 0em } 

我怎样才能解决这个问题?

回答

1

如果因为我认为,树是完全加载到内存中,你可以尝试扭转循环。这样,您只能浏览文件filein一次:

def f1(): 

    with open(filein, 'rU') as f: 
     for line in f: 
      for span in tree.iterfind('//span'): 
       if span and span.attrib.has_key('id'): 
        x = span.get('id') 
        if "af" not in x and x in line: 
          print x, line 
+0

谢谢!它完美的作品:) – user3241376 2014-11-05 15:42:39

1

发生这种情况是因为您已经读取了所有文件行,直到第二个外部循环开始。 要使其工作,你需要在FILEIN开始内环前添加f.seek(0):

with open(filein, 'rU') as f: 
    for span in tree.iterfind('//span'): 
     f.seek(0) 
     for line in f: 
      if span and span.attrib.has_key('id'): 
       x = span.get('id') 
       if "af" not in x and x in line: 
         print x, line 
+0

谢谢你的提示!我不知道这个寻找功能。 спасибо)) – user3241376 2014-11-05 15:44:04