Tuesday, 15 April 2014

python - Nested for-loop iteration stops -



python - Nested for-loop iteration stops -

i have 2 input files: html one, , css it. want produce operation on html file based on contents of css file.

my html this:

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

styles span ids defined in css file (individually each span id!)

before doing real stuff (deletion of spans based on style) trying print out ids html , style descritption css corresponding each id.

code:

from lxml import etree tree = etree.parse("file.html") filein = "file.css" def f1(): open(filein, 'ru') f: span in tree.iterfind('//span'): line in f: if span , span.attrib.has_key('id'): x = span.get('id') if "af" not in x , x in line: print x, line def main(): f1()

so, there 2 for-loops, iterate if separated, when set in function iteration stops after first loop:

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

how can prepare this?

if think, tree loaded in memory, seek reverse loops. way, browse file filein 1 time :

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

python for-loop iteration lxml

No comments:

Post a Comment