-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuttlefile
79 lines (69 loc) · 3.27 KB
/
tuttlefile
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
file://data <-
mkdir data
file://depts2015.txt <- http://www.insee.fr/fr/methodes/nomenclatures/cog/telechargement/2015/txt/depts2015.txt ! download
file://data/fr-departements.tsv <- file://depts2015.txt file://data file://articles.tsv ! python
articles = {}
with open('articles.tsv') as farticles:
for line in farticles:
code, article, charniere = line.strip().split("\t")
articles[code] = { 'article' : article, 'charniere' : charniere}
headers = ['departement_id', 'departement_name', 'article', 'charniere', 'region', 'chef_lieu']
with open('data/fr-departements.tsv', 'w') as fout, \
open('depts2015.txt') as fin :
fout.write("\t".join(headers))
fout.write("\n")
next(fin) # pass the header
for line in fin:
region, dep, cheflieu, tncc, ncc, nccenr = line.strip().split("\t")
fields = [dep, nccenr, articles[tncc]['article'], articles[tncc]['charniere'], region, cheflieu]
fout.write("\t".join(fields))
fout.write("\n")
file://reg2015.txt <- http://www.insee.fr/fr/methodes/nomenclatures/cog/telechargement/2015/txt/reg2015.txt ! download
file://data/fr-regions.tsv <- file://reg2015.txt file://data file://articles.tsv ! python
articles = {}
with open('articles.tsv') as farticles:
for line in farticles:
code, article, charniere = line.strip().split("\t")
articles[code] = { 'article' : article, 'charniere' : charniere}
headers = ['region_id', 'region_name', 'article', 'charniere', 'chef_lieu']
with open('data/fr-regions.tsv', 'w') as fout, \
open('reg2015.txt') as fin :
fout.write("\t".join(headers))
fout.write("\n")
next(fin) # pass the header
for line in fin:
region, cheflieu, tncc, ncc, nccenr = line.strip().split("\t")
fields = [region, nccenr, articles[tncc]['article'], articles[tncc]['charniere'], cheflieu]
fout.write("\t".join(fields))
fout.write("\n")
file://canton2015.txt <- http://www.insee.fr/fr/methodes/nomenclatures/cog/telechargement/2015/txt/canton2015.txt ! download
file://data/fr-cantons.tsv <- file://canton2015.txt file://data ! python
headers = ['canton_id', 'canton_name', 'type_article', 'region', 'departement']
with open('data/fr-cantons.tsv', 'w') as fout, \
open('canton2015.txt') as fin :
fout.write("\t".join(headers))
fout.write("\n")
next(fin) # pass the header
for line in fin:
region, dep, canton, typct, burcentral, \
tncc, artmaj, ncc, artmin, nccenr = line.strip().split("\t")
fields = [canton, nccenr, tncc, region, dep]
fout.write("\t".join(fields))
fout.write("\n")
file://comsimp2015.zip <- http://www.insee.fr/fr/methodes/nomenclatures/cog/telechargement/2015/txt/comsimp2015.zip ! download
file://comsimp2015.txt <- file://comsimp2015.zip
unzip comsimp2015.zip comsimp2015.txt
file://data/fr-communes.tsv <- file://comsimp2015.txt file://data ! python
headers = ['code_insee', 'town_name', 'type_article', 'region', 'departement']
with open('data/fr-communes.tsv', 'w') as fout, \
open('comsimp2015.txt') as fin :
fout.write("\t".join(headers))
fout.write("\n")
next(fin) # pass the header
for line in fin:
cdc, cheflieu, reg, dep, com, ar, ct, tncc, \
artmaj, ncc, artmin, nccenr = line.rstrip().split("\t")
code_insee = "{}{}".format(dep, com)
fields = [code_insee, nccenr, tncc, reg, dep]
fout.write("\t".join(fields))
fout.write("\n")