-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-voting-station-results-to-province-csv.py
112 lines (101 loc) · 4.11 KB
/
convert-voting-station-results-to-province-csv.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
import os
import json
import csv
from datetime import datetime
from utils import get_json_from_endpoint, save_json_to_file_in_directory
voting_stations = []
with open(f"data/voting-stations.json", "r") as file:
voting_stations = json.load(file)
print(f"Loaded {len(voting_stations)} voting stations")
country = "ZA"
provinces = {}
# Group voting stations by province
for vs in voting_stations:
province = vs["Province"]
if province not in provinces:
provinces[province] = []
provinces[province].append(vs)
for p in provinces:
pro = provinces[p]
with open(f"output/results-{p}.csv", "w", newline="") as csvfile:
csvwriter = csv.writer(csvfile, quoting=csv.QUOTE_ALL)
csvwriter.writerow(
[
"Country",
"Province",
"District",
"Municipality",
"Ward",
"VD Number",
"Registered Population",
"Spoilt Votes",
"Total Valid Votes",
"BallotType",
"sPartName",
"Party Votes",
"Released",
"Section24AVotes",
"SpecialVotes",
"PercVoterTurnout",
"TotalVotesCast",
"TotalValidVotes",
"VDCount",
"VDWithResultsCaptured",
"bResultsComplete",
]
)
for vs in provinces[p]:
province_name = vs["Province"]
vd_number = vs["VDNumber"]
district = vs["VotingDistrict"]
municipality = vs["Municipality"]
ward = vs["WardID"]
for vs_result in vs["results"]:
vd_result_event = vs["results"][vs_result]
if "RegisteredVoters" in vd_result_event:
registered_population = vd_result_event["RegisteredVoters"]
else:
registered_population = "N/A"
if "SpoiltVotes" in vd_result_event:
spoilt_votes = vd_result_event["SpoiltVotes"]
else:
spoilt_votes = "N/A"
if "TotalValidVotes" in vd_result_event:
total_valid_votes = vd_result_event["TotalValidVotes"]
else:
total_valid_votes = "N/A"
if "released_date" in vd_result_event:
released_date = vd_result_event["released_date"]
else:
released_date = "N/A"
if "PartyBallotResults" in vd_result_event:
for party in vd_result_event["PartyBallotResults"]:
csvwriter.writerow(
[
country,
province_name,
district,
municipality,
ward,
vd_number,
registered_population,
spoilt_votes,
total_valid_votes,
party["BallotType"],
party["Name"].replace(
" ", " "
), # "CONGRESS OF THE PEOPLE" has a double space
party["ValidVotes"],
released_date,
vd_result_event["Section24AVotes"],
vd_result_event["SpecialVotes"],
vd_result_event["PercVoterTurnout"],
vd_result_event["TotalVotesCast"],
vd_result_event["TotalValidVotes"],
vd_result_event["VDCount"],
vd_result_event["VDWithResultsCaptured"],
vd_result_event["bResultsComplete"],
]
)
else:
print(f"No PartyBallotResults in {vd_number}")