-
Notifications
You must be signed in to change notification settings - Fork 0
/
surnames2json.py
41 lines (35 loc) · 1.24 KB
/
surnames2json.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
from csv import DictReader
from itertools import groupby
from json import dumps
data = None
with open("common-surnames-by-country.csv", mode="r", encoding="utf-8-sig") as input:
data = list(DictReader(input))
countries = {}
for country, rows1 in groupby(data, lambda row: row["Country"]):
rows1 = list(rows1)
country_names = []
for name_group, rows2 in groupby(rows1, lambda row: row["Name Group"]):
rows2 = list(rows2)
count = max(
[int(row["Count"]) for row in rows2 if row["Count"] != ""], default=None
)
localized = list(
set([row["Localized Name"] for row in rows2 if row["Localized Name"] != ""])
)
romanized = list(
set([row["Romanized Name"] for row in rows2 if row["Romanized Name"] != ""])
)
rank = max(
[int(row["Rank"]) for row in rows2 if row["Rank"] != ""], default=None
)
country_names.append(
{
"id": name_group,
"rank": rank,
"localized": localized,
"romanized": romanized,
"count": count,
}
)
countries[country] = country_names
print(dumps(countries, ensure_ascii=False, indent=2))