-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
117 lines (97 loc) · 3.37 KB
/
main.go
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
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
)
type ElectionData struct {
Partyid int `json:"PartyID"`
Stateid int `json:"StateID"`
Candidatename string `json:"CandidateName"`
Gender string `json:"Gender"`
Age int `json:"Age"`
Politicalpartyname string `json:"PoliticalPartyName"`
Districtname string `json:"DistrictName"`
Localbodyname string `json:"LocalBodyName"`
Wardno string `json:"WardNo"`
Postname string `json:"PostName"`
Serialno int `json:"SerialNo"`
Totalvotesrecieved int `json:"TotalVotesRecieved"`
Estatus string `json:"EStatus"`
Rank int `json:"Rank"`
}
func ReadAndParseData() ([]ElectionData, error) {
jsonFile, err := os.Open("./local-level-election/raw/alldata.json")
if err != nil {
return nil, err
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var electionData []ElectionData
err = json.Unmarshal(byteValue, &electionData)
if err != nil {
return nil, err
}
return electionData, nil
}
func convertJSONToCSV(electionData []ElectionData, destination string) error {
outputFile, err := os.Create(destination)
if err != nil {
return err
}
defer outputFile.Close()
writer := csv.NewWriter(outputFile)
defer writer.Flush()
header := []string{"SerialNo", "PartyID", "StateID", "CandidateName", "Gender", "Age", "PoliticalPartyName", "DistrictName", "LocalBodyName", "WardNo", "PostName", "TotalVotesReceived", "EStatus", "Rank"}
if err := writer.Write(header); err != nil {
return err
}
for _, e := range electionData {
var csvRow []string
csvRow = append(csvRow, strconv.Itoa(e.Serialno), strconv.Itoa(e.Partyid), strconv.Itoa(e.Stateid), e.Candidatename, e.Gender, strconv.Itoa(e.Age), e.Politicalpartyname, e.Districtname, e.Localbodyname, e.Wardno, e.Postname, strconv.Itoa(e.Totalvotesrecieved), e.Estatus, strconv.Itoa(e.Rank))
if err := writer.Write(csvRow); err != nil {
return err
}
}
return nil
}
func main() {
electionData, err := ReadAndParseData()
if err != nil {
fmt.Println(err)
}
var mainMap = make(map[string][]ElectionData)
stateMap := map[string]string{
"1": "प्रदेश नं. १",
"2": "मधेश प्रदेश",
"3": "वाग्मती प्रदेश",
"4": "गण्डकी प्रदेश",
"5": "लुम्बिनी प्रदेश",
"6": "कर्णाली प्रदेश",
"7": "सुदूरपश्चिम प्रदेश",
}
for _, data := range electionData {
mainMap[fmt.Sprintf("%d__%s__%s__%s", data.Stateid, data.Districtname, data.Localbodyname, data.Postname)] = append(mainMap[fmt.Sprintf("%d__%s__%s__%s", data.Stateid, data.Districtname, data.Localbodyname, data.Postname)], data)
}
for key, value := range mainMap {
all := strings.Split(key, "__")
fileName := fmt.Sprintf("local-level-election/result/%s/%s/%s/", stateMap[all[0]], all[1], all[2])
if _, err := os.Stat(fileName); os.IsNotExist(err) {
err := os.MkdirAll(fileName, os.ModePerm)
if err != nil {
log.Println(err)
}
}
csvFileName := fmt.Sprintf("%s%s.csv", fileName, all[3])
os.Create(csvFileName)
if err := convertJSONToCSV(value, csvFileName); err != nil {
log.Fatal(err)
}
fmt.Println("fileName", fileName)
}
}