Add attribute data from a CSV to an SVG

``` python 3

import csv
import xml.etree.ElementTree as ET

dataFile = open('pharmData.csv', encoding="UTF-8")
dataReader = csv.DictReader(dataFile)
tree = ET.parse('pharmacies.xml')
root = tree.getroot()

#create list to store dat
dataSet = []

#extract data from csv
for row in dataReader:
    comuna = {}
    comuna.update({'comId':row['COMUNA']})
    comuna.update({'name':row['NOM_COMUNA']})
    comuna.update({'income':row['avg_income_tot']})
    comuna.update({'pharmCount':row['Join_Count']})
    comuna.update({'popRatio':row['POB_per_STORE']})
    dataSet.append(comuna)

#push data into xml
for polygon in root:
    polygon.set('class','comuna')
    code = polygon.get('id')
    for item in dataSet:
        if code == item['comId']:
            polygon.set('label',item['name'])
            polygon.set('income',item['income'])
            polygon.set('pharmacies',item['pharmCount'])
            polygon.set('popRatio', item['popRatio'])

#add script tag
for polygon in root:
    label = polygon.get('label')
    popRatio = polygon.get('popRatio')
    # amend data for comunas where no pharmacy was found
    if popRatio == '0':
        popRatio = 'no data'
    income = polygon.get('income')
    polygon.set('onmouseover','displayName(\''+label+'\',\''+popRatio+'\',\''+income+'\')')

#write xml
tree.write('output.xml')
Edit
Pub: 17 Apr 2020 12:53 UTC
Edit: 17 Apr 2020 13:13 UTC
Views: 341