(per Crema si parte dal vecchio post: https://bilanciocremaformatoaperto.blogspot.com/2019/02/rappresentazione-treemap-del-bilancio.html che generava l'orribile treemap: http://lucascandelli.x10host.com/prove/bilanci/prev_2019/treemapCrema2019.html)
Link al bilancio prev 2019 del comune di Isso: https://www.dati.lombardia.it/Trasparenza/COMUNE-DI-ISSO-Bilancio-di-previsione-2019-spese-/6xap-rz4h
link al formato csv:
https://www.dati.lombardia.it/api/views/6xap-rz4h/rows.csv
Si osserva che il file di Isso è "parlante": oltre ai codici MEF c'è anche il significato del codice,
pertanto nel treemap si potrà mettere il significato del codice invece del codice, così da renderlo più comprensibile agli "umani".
Il generatore del file i output sarà così (Isso usa le virgole nei campi, per cui usare tsv, non csv; inoltre usa i punti con decimali e esprime le cifre in euro non in centesimi, ci vuole FLOAT):
import csv
import requests
from string import split
url="https://www.dati.lombardia.it/api/views/6xap-rz4h/rows.tsv"
sito=requests.get(url)
listarighe=split(sito.text,"\n");
riga=[split(s,"\t") for s in listarighe]
f=open("outputisso.txt","w")
r=1
numerorighe=len(riga)
while r<len(riga)-1:
rig=riga[r]
bilanciostrut = riga[r][2]+"\t"+riga[r][4]+"\t"+riga[r][9]+"\t"+str(float(riga[r][10])/1)+"\n"
print(str(r)+" di "+ str(numerorighe)+"\t" + bilanciostrut+"\n")
f.write(bilanciostrut.encode('ascii', 'ignore'))
r +=1
f.close()
Il generatore del file html, ricavato da quello di Crema, è così:
import csv
with open('outputisso.txt', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter='\t', quotechar='|')
varTreeMap="var data = { \n '"
numrig = 0
currentc1=""
currentc2=""
previousc1=""
previousc2=""
for row in reader:
colnum = 0
for col in row:
a=row[colnum]
a=a.replace("'","`")
row[colnum]=a
colnum += 1
if not row[3] == '0':
if numrig == 0:
#inizializzo var data
print row[1]
currentc1=row[0]
currentc2=row[1]
previousc1=row[0]
previousc2=row[1]
varTreeMap+=currentc1
varTreeMap+=" ':{ \n \t \t'"
varTreeMap+=currentc2
varTreeMap+=" ':{ \n \t \t \t '"
varTreeMap+=row[2]
varTreeMap+=" ': '"
varTreeMap+=row[3]
varTreeMap+=" ', \n "
else:
#tutte le altre righe
currentc1=row[0]
currentc2=row[1]
if currentc1==previousc1:
if currentc2==previousc2:
#è un'altra foglia
varTreeMap+="\t \t \t '"
varTreeMap+=row[2]
varTreeMap+=" ': '"
varTreeMap+=row[3]
varTreeMap+=" ', \n "
else:
#cambio rametto c2
varTreeMap+="\t \t \t 'altro':'0'},\n"
varTreeMap+="\t \t '"
varTreeMap+=currentc2
varTreeMap+=" ':{ \n \t \t \t '"
varTreeMap+=row[2]
varTreeMap+=" ': '"
varTreeMap+=row[3]
varTreeMap+=" ', \n "
else:
#cambio missione c1
varTreeMap+="\t \t \t 'altro':'0' }\n"
varTreeMap+="\t },\n '"
varTreeMap+=currentc1
varTreeMap+=" ':{ \n \t '"
varTreeMap+=currentc2
varTreeMap+=" ':{ \n \t \t '"
varTreeMap+=row[2]
varTreeMap+=" ': '"
varTreeMap+=row[2]
varTreeMap+=" ', \n "
previousc1=currentc1
previousc2=currentc2
numrig += 1
varTreeMap+="\t\t\t'altro':'0' }\n\t\t}\n},"
f=open('vartreemap.txt','w')
f.write(varTreeMap.encode('ascii', 'ignore'))
f.close()
filenames = ['primopezzoissohtml.txt', 'vartreemap.txt', 'secondopezzohtml.txt']
with open('treemapIsso2019.html', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)
e il treemap di Isso è molto più leggibile: http://lucascandelli.x10host.com/prove/bilanci/prev_2019/treemapIsso2019.html
rispetto a Crema: http://lucascandelli.x10host.com/prove/bilanci/prev_2019/treemapCrema2019.html
Tutta un'altra cosa....


