Working with SVG paths in BeautifulSoup & Python
Question: I'm writing a Python script that will color in various areas of my city's
Census Block Groups (of which there are 18) different colors according to
their respective median household incomes on a map that's in the SVG format.

Sounds simple enough, right? Well, I can't figure out how, though I'm making
slight progress. What I've tried so far is making a list of each of the block
group paths according to how the SVG references them, making a list of the
median household incomes, then passing in the code that colors them. However,
this just.. doesn't seem to be working, for whatever reason. Can any of you
wonderful people help figure out where I'm misfiring?

    
    
    import csv
    from bs4 import BeautifulSoup
    
    icbg = []
    reader = csv.reader(open('censusdata.csv'),delimiter=",")
    
    #read and get income
    for row in reader:
       income = row[6]
       income = int(income)
       icbg.append(income)
    
    svg = open('NM2.svg','r')
    soup = BeautifulSoup(svg,"lxml")
    
    #find CBGs and incomes
    path1 = soup.find('path')
    path2 = path1.find_next('path')
    path3 = path2.find_next('path')
    path4 = path3.find_next('path')
    path5 = path4.find_next('path')
    path6 = path5.find_next('path')
    path7 = path6.find_next('path')
    path8 = path7.find_next('path')
    path9 = path8.find_next('path')
    path10 = path9.find_next('path')
    path11 = path10.find_next('path')
    path12 = path11.find_next('path')
    path13 = path12.find_next('path')
    path14 = path13.find_next('path')
    path15 = path14.find_next('path')
    path16 = path15.find_next('path')
    path17 = path16.find_next('path')
    path18 = path17.find_next('path')
    incomep1 = icbg[0]
    incomep2 = icbg[1]
    incomep3 = icbg[2]
    incomep4 = icbg[3]
    incomep5 = icbg[4]
    incomep6 = icbg[5]
    incomep7 = icbg[6]
    incomep8 = icbg[7]
    incomep9 = icbg[8]
    incomep10 = icbg[9]
    incomep11 = icbg[10]
    incomep12 = icbg[11]
    incomep13 = icbg[12]
    incomep14 = icbg[13]
    incomep15 = icbg[14]
    incomep16 = icbg[15]
    incomep17 = icbg[16]
    incomep18 = icbg[17]
    
    paths = (path1, path2, path3, path4, path5, path6, path7, path8, path9, path10,
                 path11, path12, path13, path14, path15, path16, path17, path18)
    incomes = (incomep1,incomep2,incomep3,incomep4,incomep5,incomep6,incomep7,incomep8,
                   incomep9,incomep10,incomep11,incomep12,incomep13,incomep14,incomep15,incomep16,incomep17,incomep18)
    
    #set colors
    colors = ['fee5d9','fcae91','fb6a4a','de2d26','a50f15']
    
    for p in paths:
        for i in range(0,17):
            it = incomes[i]
            if it > 20000:
                color_class = 2
            elif it > 25000:
                color_class = 1
            elif it > 30000:
                color_class = 3
            elif it > 35000:
                color_class = 4
    
            color = colors[color_class]
            path_style = "font-size:12px;fill:#%s;fill-rule:nonzero;stroke:#000000;stroke-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel" % color
            p['style'] = path_style
        print(soup.prettify())
    

Running this gives me an SVG file like so: `fill:#fb6a4a;fill-
rule:nonzero;stroke:#000000;stroke-opacity:1;stroke-width:0.1;stroke-
miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-
start:none;stroke-linejoin:bevel">` comes up 18 times, meaning for every
available path, even though these paths have different incomes.

could the problem be with the way I wrote my comparisons?


Answer: From my understanding of what you are trying to do, your problem is that you
have 2 `for` loops instead of one. You should loop through the paths and
incomes at the same time. The way you are doing it now is you are looping
through all the incomes for each path. The following code simply moves the
paths into the same loop as the income so they are looped through at the same
time.

    
    
    for i in range(0,17):
        it = incomes[i]
        p = paths[i]
        if it > 20000:
            color_class = 2
        elif it > 25000:
            color_class = 1
        elif it > 30000:
            color_class = 3
        elif it > 35000:
            color_class = 4
    
        color = colors[color_class]
        path_style = "font-size:12px;fill:#%s;fill-rule:nonzero;stroke:#000000;stroke-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel" % color
        p['style'] = path_style
    

