简体   繁体   English

在BeautifulSoup和Python中使用SVG路径

[英]Working with SVG paths in BeautifulSoup & Python

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. 我正在编写一个Python脚本,它将根据SVG格式的地图上各自家庭中位收入的不同,在城市普查区组(共有18种)的各个区域着色。

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. 到目前为止,我一直在尝试根据SVG如何引用它们来列出每个块组路径的列表,列出家庭收入中位数,然后传入为其着色的代码。 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. 运行此命令将得到一个SVG文件,如下所示: 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">上升了18次,这意味着对于每个可用路径,即使这些路径具有不同的收入。

could the problem be with the way I wrote my comparisons? 问题可能出在我编写比较的方式上吗?

From my understanding of what you are trying to do, your problem is that you have 2 for loops instead of one. 根据我对要执行的操作的了解,您的问题是您有2个for循环而不是1个。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM