Created
October 25, 2023 12:29
-
-
Save timendum/ef26c410dbbf92aca058fa30589bfdd2 to your computer and use it in GitHub Desktop.
contanomi to csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
dcsv = {"0": {}, "1": {}} | |
for y in range(1999, 2022): | |
r = requests.get(f"https://www.istat.it/ws/nati/index2021.php?type=list&limit=20&year={y}") | |
rdata = json.loads(r.text[9:-2]) | |
for k in ("0", "1"): | |
for row in rdata[k]: | |
dcsv[k][row["name"]] = dcsv[k].get(row["name"], {}) | |
dcsv[k][row["name"]][int(row["year"])] = { | |
k: float(v) for k, v in row.items() if k in ("percent", "count") | |
} | |
for k in ("0", "1"): | |
gender = {"0": "m", "1": "f"}[k] | |
for name in dcsv[k]: | |
if len(dcsv[k][name]) == 23: | |
continue | |
r = requests.get( | |
f"https://www.istat.it/ws/nati/index2021.php?callback=callback&type=name&name={name}&gender={gender}&originalName={name}" | |
) | |
rdata = json.loads(r.text[9:-2]) | |
for row in rdata.values(): | |
if len(row) > 1: | |
continue | |
row = row[0] | |
if not row["year"]: | |
continue | |
dcsv[k][name][int(row["year"])] = { | |
k: float(v or "0") for k, v in row.items() if k in ("percent", "count") | |
} | |
import csv | |
with open("percent.csv", "w", newline="") as csvfile: | |
cwriter = csv.writer(csvfile, delimiter=",", quotechar="|", quoting=csv.QUOTE_MINIMAL) | |
for k in ("0", "1"): | |
gender = {"0": "m", "1": "f"}[k] | |
for name in dcsv[k]: | |
row = [name, gender] | |
for y in range(1999, 2022): | |
if y in dcsv[k][name]: | |
row.append(dcsv[k][name][y]["percent"]) | |
else: | |
row.append("") | |
cwriter.writerow(row) | |
with open("count.csv", "w", newline="") as csvfile: | |
cwriter = csv.writer(csvfile, delimiter=",", quotechar="|", quoting=csv.QUOTE_MINIMAL) | |
for k in ("0", "1"): | |
gender = {"0": "m", "1": "f"}[k] | |
for name in dcsv[k]: | |
row = [name, gender] | |
for y in range(1999, 2022): | |
if y in dcsv[k][name]: | |
row.append(dcsv[k][name][y]["count"]) | |
else: | |
row.append("") | |
cwriter.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment