-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvHandler.py
More file actions
131 lines (116 loc) · 5.61 KB
/
csvHandler.py
File metadata and controls
131 lines (116 loc) · 5.61 KB
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
import csv
from pathlib import Path
from collections import defaultdict
from typing import Literal
from constants import POST_MAP
class CSVHandler:
""" Convert Json File into CSV file """
STATE_COLUMNS = [
"Rank", "CandidateName", "PoliticalPartyName", "PartySymbol",
"TotalVoteReceived", "Gender", "Age", "Status",
"StateName", "DistrictName", "Constituency",
"QUALIFICATION", "EXPERIENCE", "OTHERDETAILS", "NAMEOFINST"
]
PROV_COLUMNS = STATE_COLUMNS + ["ProvicalConstituency"]
LOCAL_COLUMNS=["CandidateName", "PoliticalPartyName", "PartySymbol",
"TotalVoteReceived", "Gender", "Age", "Status","Ward"]
def __init__(self,json_data,type:Literal["state-election","provincal-election","local-election"]):
self.json_data=json_data
self.type=type
self.grouped_data=None
def parse_json(self):
self.grouped_data = defaultdict(list)
if self.type == "state-election":
for data in self.json_data:
key = (data["StateName"], data["DistrictName"], data["SCConstID"])
self.grouped_data[key].append(data)
elif self.type == "provincal-election":
for data in self.json_data:
key = (
data["StateName"],
data["DistrictName"],
data["SCConstID"],
data["CenterConstID"],
)
self.grouped_data[key].append(data)
else:
for data in self.json_data:
key = (
data["StateName"],
data["DistrictName"],
data["LocalLevelName"],
)
self.grouped_data[key].append(data)
def _map_row(self,candidate):
if self.type == "local-election":
return {
"CandidateName": candidate.get("CandidateName"),
"PoliticalPartyName": candidate.get("PoliticalPartyName"),
"PartySymbol": candidate.get("SymbolName"),
"TotalVoteReceived": int(candidate.get("TotalVoteReceived", 0)),
"Gender": candidate.get("Gender"),
"Age": candidate.get("Age"),
"Status": candidate.get("RemarksEng"),
"Ward":candidate.get("Ward",""), }
row = {
"Rank": candidate.get("Rank"),
"CandidateName": candidate.get("CandidateName"),
"PoliticalPartyName": candidate.get("PoliticalPartyName"),
"PartySymbol": candidate.get("SymbolName"), # map SymbolName to PartySymbol
"TotalVoteReceived": int(candidate.get("TotalVoteReceived")),
"Gender": candidate.get("Gender"),
"Age": candidate.get("Age"),
"Status":candidate.get("Remarks"),
"StateName": candidate.get("StateName"),
"DistrictName": candidate.get("DistrictName"),
"Constituency": candidate.get("SCConstID"),
"QUALIFICATION": candidate.get("QUALIFICATION"),
"EXPERIENCE": candidate.get("EXPERIENCE"),
"OTHERDETAILS": candidate.get("OTHERDETAILS"),
"NAMEOFINST": candidate.get("NAMEOFINST"),
}
if self.type == "provincal-election":
row["ProvicalConstituency"] = candidate.get("CenterConstID")
return row
def store_to_csv(self,filename=None):
if self.type == "state-election":
columns = self.STATE_COLUMNS
elif self.type == "provincal-election":
columns = self.PROV_COLUMNS
else:
columns = self.LOCAL_COLUMNS
for key, candidates in self.grouped_data.items():
if self.type=="state-election":
state,district,const = key
path = Path(self.type)/state/district
filename = path/f"{const}.csv"
elif self.type=="provincal-election":
state,district,const,prov = key
path = Path(self.type)/state/district/const
filename = path/f"{prov}.csv"
elif self.type =="local-election":
state,district,local = key
path = Path(self.type)/state/district/local
path.mkdir(parents=True, exist_ok=True)
post_groups = defaultdict(list)
for c in candidates:
post_id = c.get("PostId")
if post_id not in POST_MAP:
continue
post_groups[post_id].append(c)
for post_id, post_candidates in post_groups.items():
post_name = POST_MAP[post_id]
file_path = path / f"{post_name}.csv"
with open(file_path, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=columns)
writer.writeheader()
for c in post_candidates:
writer.writerow(self._map_row(c))
continue
if self.type != "local-election":
path.mkdir(parents=True, exist_ok=True)
with open(filename,"w",newline="",encoding="utf-8") as f:
writer = csv.DictWriter(f,fieldnames=columns)
writer.writeheader()
for c in candidates:
writer.writerow(self._map_row(c))