-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (80 loc) · 2.23 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
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
type ElectionData struct {
Provience string `json:"Provience"`
District string `json:"District"`
LocalBody string `json:"Local Body"`
WardNo string `json:"Ward No"`
Post string `json:"Post"`
Candidate string `json:"Candidate"`
Party string `json:"Party"`
}
func ReadAndParseData() ([]ElectionData, error) {
jsonFile, err := os.Open("./data/candidate_list.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{"Provience", "District", "Local Body", "Ward No", "Post", "Candidate", "Party"}
if err := writer.Write(header); err != nil {
return err
}
for _, e := range electionData {
var csvRow []string
csvRow = append(csvRow, e.Provience, e.District, e.LocalBody, e.WardNo, e.Post, e.Candidate, e.Party)
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)
for _, data := range electionData {
mainMap[fmt.Sprintf("%s__%s__%s__%s", data.Provience, data.District, data.LocalBody, data.Post)] = append(mainMap[fmt.Sprintf("%s__%s__%s__%s", data.Provience, data.District, data.LocalBody, data.Post)], data)
}
for key, value := range mainMap {
all := strings.Split(key, "__")
fileName := fmt.Sprintf("list/%s/%s/%s/", 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)
}
}