Python matching blocks of text
I have a rather large kml file and it is full of a bunch of styles and stylemaps that are duplicates of each other with the only difference usually being the <style id="XXXXX">. I am writing a script that was supposed to find all the style maps, find all the style id's, go through all the contents of each style id, find if the content matches any other style id's content and basically output a style id and all the style id's that have the same content. From there I am going to get rid of all the dupes, which I think I will be able to handle coding, but haven't done yet.

I am not a very experienced coder and I know my code is probably pretty sloppy, illogical and and the variables are probably named poorly. Sorry.  The code works (does exactly what I want it to) all the way up to the last section of for loops. Any help would be appreciated, thanks!


    from sys import argv

    script, infile, outfile = argv



    source = open(infile)
    target = open(outfile, 'w')

    style_map_elements = []
    style_map_contents = {}

    Style_Map_found = False

    # FIND ALL THE STYLE MAPS ouput their contents to to style_map_contents dict
    for line in source.readlines():
        if Style_Map_found is True:
            Current_style_map_contents.append(line)
    
        if Style_Map_found is True and "</StyleMap>" in line:
            Style_Map_found = False
            style_map_contents[Current_StyleMap_ID] = Current_style_map_contents
           
        if "StyleMap id" in line:
            print "Found a styleMap! " + line
            Current_StyleMap_ID = line.strip('\t<StyleMap id=>\r\n')
            Current_StyleMap_ID = Current_StyleMap_ID.strip('"')
            style_map_elements.append(Current_StyleMap_ID)
            Current_style_map_contents = []
            Current_style_map_contents.append(line)
            Style_Map_found = True

     # Go through all the style_map_contents and extract the pair of style ID's they  contain, put them in Style_Maps_IDs dict        
    count = 0
    Style_Maps_IDs = {}
    Style_Maps_ID = []
    for map_element in style_map_elements:
        for style_content in style_map_contents[map_element]:
            if '<styleUrl>' in style_content:                 
                Style_Maps_ID.append(style_content)
            
                count += 1
        Style_Maps_IDs[map_element] = Style_Maps_ID
        Style_Maps_ID = []


    # Cycle through the Style ID's gotten from the Style maps. Find where those style id's are defined and grab their contents. Puts each style ID's contents in the style_ID_contents dict

    source.seek(0)
    style_ID_elements = []
    style_ID_contents = {}

    Style_ID_found = False

    for line in source.readlines():
        if  Style_ID_found is True:
            print "STYLE ID FOUND IS TRUE!"
            Current_Style_ID_contents.append(line)
        
        if Style_ID_found is True and "/Style" in line:
            print "Found end of style ID!"
            Style_ID_found = False
            style_ID_contents[Current_Style_ID] = Current_Style_ID_contents   
        
        if "Style id" in line:
            print "Found a Style ID!"
            Current_Style_ID = line.strip('\t<Style id=>\r\n')
            Current_Style_ID = Current_Style_ID.strip('"')
            style_ID_elements.append(Current_Style_ID)
            Current_Style_ID_contents = []
            Current_Style_ID_contents.append(line)
            Style_ID_found = True
        
    x = len(style_map_elements)

    #This takes each style_ID dict entry, purges it of \r\n\t, and puts it in a matcher dict
    current_matcher = []
    matcher = {}
    for style in style_ID_elements:
        current_matcher = []
        for style_content in style_ID_contents[style]:
            style_content = style_content.replace('\t', '')
            style_content = style_content.replace('\r', '')        
            style_content = style_content.replace('\n', '')        
            current_matcher.append(style_content)
        matcher[style] = current_matcher




    #This is where the problem lies for me. Up to this point the code does everything I want it to do. All the dicts contain their proper data.  This code is supposed to open each of the entries in the matcher dict (which has been stripped of tabs and new lines), skip line 1, see if #each line matches all lines in each dict entry in style_ID_elements and then write out to a file what styles match what styles. 

    match = False
    match_line = 0
    for style in style_ID_elements:
        match_line = 0
        for match_source in matcher[style]:
            for style_search in style_ID_elements:
                 for style_content in style_ID_contents[style_search]:
                    if match_line > 0 and match_source in style_content:
                        print "It matches!"
                        match = True
                        match_line += 1
                    if match is True and "</Style>" in match_source:
                        found = 'Matched %s to %s' % (style, style_search)
                        target.write(found)
                        match = False
                        match_line = 0
                    elif match_line > 0 and match is False:
                        print "Doesn't match!"
                        match_line += 1 
                        match = False
                        break    
                    else:
                        print "First line doesn't match, but we know that it won't, because  it is the style ID, lets check line 2"
                        match_line += 1     
         

           
    print x        
    print count

    source.close()
    target.close()


 
Python matching blocks of text
===

I have a rather large kml file and it is full of a bunch of styles and stylemaps that are duplicates of each other with the only difference usually being the <style id="XXXXX">. I am writing a script that was supposed to find all the style maps, find all the style id's, go through all the contents of each style id, find if the content matches any other style id's content and basically output a style id and all the style id's that have the same content. From there I am going to get rid of all the dupes, which I think I will be able to handle coding, but haven't done yet.

I am not a very experienced coder and I know my code is probably pretty sloppy, illogical and and the variables are probably named poorly. Sorry.  The code works (does exactly what I want it to) all the way up to the last section of for loops. Any help would be appreciated, thanks!


    from sys import argv

    script, infile, outfile = argv



    source = open(infile)
    target = open(outfile, 'w')

    style_map_elements = []
    style_map_contents = {}

    Style_Map_found = False

    # FIND ALL THE STYLE MAPS ouput their contents to to style_map_contents dict
    for line in source.readlines():
        if Style_Map_found is True:
            Current_style_map_contents.append(line)
    
        if Style_Map_found is True and "</StyleMap>" in line:
            Style_Map_found = False
            style_map_contents[Current_StyleMap_ID] = Current_style_map_contents
           
        if "StyleMap id" in line:
            print "Found a styleMap! " + line
            Current_StyleMap_ID = line.strip('\t<StyleMap id=>\r\n')
            Current_StyleMap_ID = Current_StyleMap_ID.strip('"')
            style_map_elements.append(Current_StyleMap_ID)
            Current_style_map_contents = []
            Current_style_map_contents.append(line)
            Style_Map_found = True

     # Go through all the style_map_contents and extract the pair of style ID's they  contain, put them in Style_Maps_IDs dict        
    count = 0
    Style_Maps_IDs = {}
    Style_Maps_ID = []
    for map_element in style_map_elements:
        for style_content in style_map_contents[map_element]:
            if '<styleUrl>' in style_content:                 
                Style_Maps_ID.append(style_content)
            
                count += 1
        Style_Maps_IDs[map_element] = Style_Maps_ID
        Style_Maps_ID = []


    # Cycle through the Style ID's gotten from the Style maps. Find where those style id's are defined and grab their contents. Puts each style ID's contents in the style_ID_contents dict

    source.seek(0)
    style_ID_elements = []
    style_ID_contents = {}

    Style_ID_found = False

    for line in source.readlines():
        if  Style_ID_found is True:
            print "STYLE ID FOUND IS TRUE!"
            Current_Style_ID_contents.append(line)
        
        if Style_ID_found is True and "/Style" in line:
            print "Found end of style ID!"
            Style_ID_found = False
            style_ID_contents[Current_Style_ID] = Current_Style_ID_contents   
        
        if "Style id" in line:
            print "Found a Style ID!"
            Current_Style_ID = line.strip('\t<Style id=>\r\n')
            Current_Style_ID = Current_Style_ID.strip('"')
            style_ID_elements.append(Current_Style_ID)
            Current_Style_ID_contents = []
            Current_Style_ID_contents.append(line)
            Style_ID_found = True
        
    x = len(style_map_elements)

    #This takes each style_ID dict entry, purges it of \r\n\t, and puts it in a matcher dict
    current_matcher = []
    matcher = {}
    for style in style_ID_elements:
        current_matcher = []
        for style_content in style_ID_contents[style]:
            style_content = style_content.replace('\t', '')
            style_content = style_content.replace('\r', '')        
            style_content = style_content.replace('\n', '')        
            current_matcher.append(style_content)
        matcher[style] = current_matcher




    #This is where the problem lies for me. Up to this point the code does everything I want it to do. All the dicts contain their proper data.  This code is supposed to open each of the entries in the matcher dict (which has been stripped of tabs and new lines), skip line 1, see if #each line matches all lines in each dict entry in style_ID_elements and then write out to a file what styles match what styles. 

    match = False
    match_line = 0
    for style in style_ID_elements:
        match_line = 0
        for match_source in matcher[style]:
            for style_search in style_ID_elements:
                 for style_content in style_ID_contents[style_search]:
                    if match_line > 0 and match_source in style_content:
                        print "It matches!"
                        match = True
                        match_line += 1
                    if match is True and "</Style>" in match_source:
                        found = 'Matched %s to %s' % (style, style_search)
                        target.write(found)
                        match = False
                        match_line = 0
                    elif match_line > 0 and match is False:
                        print "Doesn't match!"
                        match_line += 1 
                        match = False
                        break    
                    else:
                        print "First line doesn't match, but we know that it won't, because  it is the style ID, lets check line 2"
                        match_line += 1     
         

           
    print x        
    print count

    source.close()
    target.close()


 