-
Notifications
You must be signed in to change notification settings - Fork 74
/
update_comuni.py
131 lines (101 loc) · 3.97 KB
/
update_comuni.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
import urllib.request
from os import path, curdir
import csv
import re
import os
# PERMALINK : do not change without a reason
ISTAT_DATA_URL = "https://www.istat.it/storage/codici-unita-amministrative/Elenco-comuni-italiani.csv"
ISTAT_FILE_NAME = "Elenco-comuni-italiani.csv"
ISTAT_FILE_PATH = path.join(curdir, ISTAT_FILE_NAME)
COMUNI_FILE_NAME = "lista-comuni.js"
COMUNI_FILE_PATH = path.join(curdir, 'src', COMUNI_FILE_NAME)
PROVINCE_FILE_NAME = "lista-province.js"
PROVINCE_FILE_PATH = path.join(curdir, 'src', PROVINCE_FILE_NAME)
print('Scarico i dati...')
urllib.request.urlretrieve(ISTAT_DATA_URL, ISTAT_FILE_PATH)
print('Download completato')
print('Lettura dei nuovi dati in corso')
nuova_lista_comuni = []
nuova_lista_province = []
with open(ISTAT_FILE_PATH) as csvfile:
reader = csv.DictReader(csvfile, delimiter=';')
#next(reader, None) #skip the header
for row in reader:
cod = row['Codice Catastale del comune']
prov = row['Sigla automobilistica']
name = row["Denominazione in italiano"]
# Remove accents and use uppercase for comune's name
name = name.lower()
# Remove a the end of a word
name = re.compile(r"(à|á)(?=\s|$|-)").sub( "a'", name)
name = re.compile(r"(è|é)(?=\s|$|-)").sub( "e'", name)
name = re.compile(r"(ì|í)(?=\s|$|-)").sub( "i'", name)
name = re.compile(r"(ò|ó)(?=\s|$|-)").sub( "o'", name)
name = re.compile(r"(ù|ú)(?=\s|$|-)").sub( "u'", name)
#Remove inside a word
name = name.translate(str.maketrans("àáâêèéìíôòóùúç","aaaeeeiiooouuc"))
#make everything uppercase and remove empty spaces
name = name.upper().strip()
nuova_lista_comuni.append([ cod, prov, name])
print("Confronto i nuovi dati con quelli presenti")
with open(COMUNI_FILE_PATH, 'r') as f:
txt = f.read()
txt = txt.replace("export const COMUNI = ", "")
lista_comuni = eval(txt)
for comune in lista_comuni:
del comune[3]
with open(PROVINCE_FILE_PATH, 'r') as f:
txt = f.read()
txt = txt.replace("export const PROVINCE = ", "").replace(':', '":').replace(",\n",",\n\"").replace("{\n","{\n\"")
lista_province = eval(txt+"\n").keys()
nuove_province = []
nuovi_comuni = []
for comune in nuova_lista_comuni:
if comune not in lista_comuni:
lista_comuni.append(comune)
nuovi_comuni.append(comune)
if comune[1] not in lista_province:
nuove_province.append(comune[1])
for comune in lista_comuni:
if comune[:3] in nuova_lista_comuni:
try:
comune[3] = 1
except:
comune.append(1)
else:
try:
comune[3] = 0
except:
comune.append(0)
nuove_province = list(set(nuove_province)) # Remove duplicates
print(f"Trovati {len(nuovi_comuni)} nuovi comuni")
print(f"Trovati {len(nuove_province)} nuove province")
# Nuove province
if len(nuove_province):
NUOVE_PROVINCE_FILE_NAME = "nuove_province.txt"
NUOVE_PROVINCE_FILE_PATH = path.join(curdir, NUOVE_PROVINCE_FILE_NAME)
print(f"Le sigle delle nuove province sono salvate nel file {NUOVE_PROVINCE_FILE_NAME}")
with open(NUOVE_PROVINCE_FILE_PATH, 'w') as f:
for provincia in nuove_province:
print(provincia, file=f)
f.close()
# Sorting
comuni_txt = list(map(lambda x: "\r,\r".join([x[2],x[0],x[1],str(x[3])]), lista_comuni))
comuni_txt.sort()
with open(COMUNI_FILE_PATH,'w') as f:
f.write("export const COMUNI = [ ")
last_idx = len(comuni_txt) - 1
for i, el in enumerate(comuni_txt):
comps = el.split("\r,\r")
if " * " in comps[0]:
comps[0] = comps[0].split(" * ")[0]
f.write(f"[\"{comps[1]}\",\"{comps[2]}\",\"{comps[0]}\",{comps[3]}]")
if i != last_idx:
f.write(",")
f.write("\n")
f.write("]")
print(f"File {COMUNI_FILE_NAME} aggiornato")
# Clean up
os.remove(ISTAT_FILE_PATH)