From 6003765d39c2ea5bffb3284830c566e7080847f4 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 9 Jul 2017 10:03:55 +0200 Subject: [PATCH] CSV to JSON --- .gitignore | 2 + financial.json | 1 + financialjson/main.go | 112 ++++++++++++++++++++++++++++++ state_table.json | 1 + statecsv2json/main.go | 157 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 273 insertions(+) create mode 100644 financial.json create mode 100644 financialjson/main.go create mode 100644 state_table.json create mode 100644 statecsv2json/main.go diff --git a/.gitignore b/.gitignore index e2ee7a1..a196bf1 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,5 @@ shadirconcurrent/shadirconcurrent pinger/pinger datediff/datediff example-json/example-json +financialjson/financialjson +statecsv2json/statecsv2json diff --git a/financial.json b/financial.json new file mode 100644 index 0000000..51241d2 --- /dev/null +++ b/financial.json @@ -0,0 +1 @@ +[{"Date":"2017-06-08T00:00:00Z","Open":982.349976,"High":984.570007,"Low":977.200012,"Close":983.409973,"Adj_Close":983.409973,"Volume":1481900},{"Date":"2017-06-09T00:00:00Z","Open":984.5,"High":984.5,"Low":935.630005,"Close":949.830017,"Adj_Close":949.830017,"Volume":3309400},{"Date":"2017-06-12T00:00:00Z","Open":939.559998,"High":949.35498,"Low":915.232971,"Close":942.900024,"Adj_Close":942.900024,"Volume":3763500},{"Date":"2017-06-13T00:00:00Z","Open":951.909973,"High":959.97998,"Low":944.090027,"Close":953.400024,"Adj_Close":953.400024,"Volume":2013300},{"Date":"2017-06-14T00:00:00Z","Open":959.919983,"High":961.150024,"Low":942.25,"Close":950.76001,"Adj_Close":950.76001,"Volume":1489700},{"Date":"2017-06-15T00:00:00Z","Open":933.969971,"High":943.338989,"Low":924.440002,"Close":942.309998,"Adj_Close":942.309998,"Volume":2133100},{"Date":"2017-06-16T00:00:00Z","Open":940,"High":942.039978,"Low":931.594971,"Close":939.780029,"Adj_Close":939.780029,"Volume":3094700},{"Date":"2017-06-19T00:00:00Z","Open":949.960022,"High":959.98999,"Low":949.049988,"Close":957.369995,"Adj_Close":957.369995,"Volume":1533300},{"Date":"2017-06-20T00:00:00Z","Open":957.52002,"High":961.619995,"Low":950.01001,"Close":950.630005,"Adj_Close":950.630005,"Volume":1126000},{"Date":"2017-06-21T00:00:00Z","Open":953.640015,"High":960.099976,"Low":950.76001,"Close":959.450012,"Adj_Close":959.450012,"Volume":1202200},{"Date":"2017-06-22T00:00:00Z","Open":958.700012,"High":960.719971,"Low":954.549988,"Close":957.090027,"Adj_Close":957.090027,"Volume":941400},{"Date":"2017-06-23T00:00:00Z","Open":956.830017,"High":966,"Low":954.200012,"Close":965.590027,"Adj_Close":965.590027,"Volume":1527900},{"Date":"2017-06-26T00:00:00Z","Open":969.900024,"High":973.309998,"Low":950.789978,"Close":952.27002,"Adj_Close":952.27002,"Volume":1598400},{"Date":"2017-06-27T00:00:00Z","Open":942.460022,"High":948.289978,"Low":926.849976,"Close":927.330017,"Adj_Close":927.330017,"Volume":2579900},{"Date":"2017-06-28T00:00:00Z","Open":929,"High":942.75,"Low":916,"Close":940.48999,"Adj_Close":940.48999,"Volume":2721400},{"Date":"2017-06-29T00:00:00Z","Open":929.919983,"High":931.26001,"Low":910.619995,"Close":917.789978,"Adj_Close":917.789978,"Volume":3299200},{"Date":"2017-06-30T00:00:00Z","Open":926.049988,"High":926.049988,"Low":908.309998,"Close":908.72998,"Adj_Close":908.72998,"Volume":2090200},{"Date":"2017-07-03T00:00:00Z","Open":912.179993,"High":913.940002,"Low":894.789978,"Close":898.700012,"Adj_Close":898.700012,"Volume":1709800},{"Date":"2017-07-05T00:00:00Z","Open":901.76001,"High":914.51001,"Low":898.5,"Close":911.710022,"Adj_Close":911.710022,"Volume":1813900},{"Date":"2017-07-06T00:00:00Z","Open":904.119995,"High":914.94397,"Low":899.700012,"Close":906.690002,"Adj_Close":906.690002,"Volume":1415200},{"Date":"2017-07-07T00:00:00Z","Open":908.849976,"High":921.539978,"Low":908.849976,"Close":918.590027,"Adj_Close":918.590027,"Volume":1637785}] diff --git a/financialjson/main.go b/financialjson/main.go new file mode 100644 index 0000000..ec09229 --- /dev/null +++ b/financialjson/main.go @@ -0,0 +1,112 @@ +package main + +import "encoding/csv" +import "encoding/json" +import "time" +import "io" +import "log" +import "os" +import "strconv" + +var COLUMNS []string + +type FinancialData struct { + Date time.Time + Open, High, Low, Close, Adj_Close, Volume float64 +} + +func readCsvLine(line []string) *FinancialData { + data := new(FinancialData) + for i, col := range line { + switch COLUMNS[i] { + case "Date": + d, err := time.Parse("2006-01-02", col) + if err != nil { + panic(err) + } else { + data.Date = d + } + case "Open": + data.Open = toFloat(col) + case "High": + data.High = toFloat(col) + case "Low": + data.Low = toFloat(col) + case "Close": + data.Close = toFloat(col) + case "Adj Close": + data.Adj_Close = toFloat(col) + case "Volume": + data.Volume = toFloat(col) + } + } + return data +} + +func toFloat(v string) float64 { + f, err := strconv.ParseFloat(v, 64) + if err != nil { + log.Fatalln(err) + } + return f +} + +func readCsvHeader(line []string) { + for _, col := range line { + COLUMNS = append(COLUMNS, col) + } +} + +func readCsv(in io.Reader) []FinancialData { + data := []FinancialData{} + csvReader := csv.NewReader(in) + for { + line, err := csvReader.Read() + if err == io.EOF { + break + } else if err != nil { + log.Fatal(err) + } + if len(COLUMNS) == 0 { + readCsvHeader(line) + } else { + data = append(data, *readCsvLine(line)) + } + } + return data + +} + +func main() { + if len(os.Args) < 2 { + log.Fatalln("Usage go-financial ") + } + + // read input + csv, err := os.Open(os.Args[1]) + if err != nil { + log.Fatalln("Error reading file", err) + } + defer csv.Close() + + // parse data + data := readCsv(csv) + + // convert JSON + var writer io.Writer + if len(os.Args) > 2 { + f, ferr := os.Create(os.Args[2]) + if ferr != nil { + panic(ferr) + } + defer f.Close() + writer = f + } else { + writer = os.Stdout + } + err = json.NewEncoder(writer).Encode(data) + if err != nil { + panic(err) + } + +} diff --git a/state_table.json b/state_table.json new file mode 100644 index 0000000..e108d66 --- /dev/null +++ b/state_table.json @@ -0,0 +1 @@ +{"AK":{"Id":2,"Name":"Alaska","Abbreviation":"AK","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"2","Assoc_press":"Alaska","Standard_federal_region":"X","Census_region":4,"Census_region_name":"West","Census_division":9,"Census_division_name":"Pacific","Circuit_court":9},"AL":{"Id":1,"Name":"Alabama","Abbreviation":"AL","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"1","Assoc_press":"Ala.","Standard_federal_region":"IV","Census_region":3,"Census_region_name":"South","Census_division":6,"Census_division_name":"East South Central","Circuit_court":11},"AR":{"Id":4,"Name":"Arkansas","Abbreviation":"AR","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"5","Assoc_press":"Ark.","Standard_federal_region":"VI","Census_region":3,"Census_region_name":"South","Census_division":7,"Census_division_name":"West South Central","Circuit_court":8},"AZ":{"Id":3,"Name":"Arizona","Abbreviation":"AZ","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"4","Assoc_press":"Ariz.","Standard_federal_region":"IX","Census_region":4,"Census_region_name":"West","Census_division":8,"Census_division_name":"Mountain","Circuit_court":9},"CA":{"Id":5,"Name":"California","Abbreviation":"CA","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"6","Assoc_press":"Calif.","Standard_federal_region":"IX","Census_region":4,"Census_region_name":"West","Census_division":9,"Census_division_name":"Pacific","Circuit_court":9},"CO":{"Id":6,"Name":"Colorado","Abbreviation":"CO","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"8","Assoc_press":"Colo.","Standard_federal_region":"VIII","Census_region":4,"Census_region_name":"West","Census_division":8,"Census_division_name":"Mountain","Circuit_court":10},"CT":{"Id":7,"Name":"Connecticut","Abbreviation":"CT","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"9","Assoc_press":"Conn.","Standard_federal_region":"I","Census_region":1,"Census_region_name":"Northeast","Census_division":1,"Census_division_name":"New England","Circuit_court":2},"DE":{"Id":8,"Name":"Delaware","Abbreviation":"DE","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"10","Assoc_press":"Del.","Standard_federal_region":"III","Census_region":3,"Census_region_name":"South","Census_division":5,"Census_division_name":"South Atlantic","Circuit_court":3},"FL":{"Id":9,"Name":"Florida","Abbreviation":"FL","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"12","Assoc_press":"Fla.","Standard_federal_region":"IV","Census_region":3,"Census_region_name":"South","Census_division":5,"Census_division_name":"South Atlantic","Circuit_court":11},"GA":{"Id":10,"Name":"Georgia","Abbreviation":"GA","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"13","Assoc_press":"Ga.","Standard_federal_region":"IV","Census_region":3,"Census_region_name":"South","Census_division":5,"Census_division_name":"South Atlantic","Circuit_court":11},"HI":{"Id":11,"Name":"Hawaii","Abbreviation":"HI","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"15","Assoc_press":"Hawaii","Standard_federal_region":"IX","Census_region":4,"Census_region_name":"West","Census_division":9,"Census_division_name":"Pacific","Circuit_court":9},"IA":{"Id":15,"Name":"Iowa","Abbreviation":"IA","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"19","Assoc_press":"Iowa","Standard_federal_region":"VII","Census_region":2,"Census_region_name":"Midwest","Census_division":4,"Census_division_name":"West North Central","Circuit_court":8},"ID":{"Id":12,"Name":"Idaho","Abbreviation":"ID","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"16","Assoc_press":"Idaho","Standard_federal_region":"X","Census_region":4,"Census_region_name":"West","Census_division":8,"Census_division_name":"Mountain","Circuit_court":9},"IL":{"Id":13,"Name":"Illinois","Abbreviation":"IL","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"17","Assoc_press":"Ill.","Standard_federal_region":"V","Census_region":2,"Census_region_name":"Midwest","Census_division":3,"Census_division_name":"East North Central","Circuit_court":7},"IN":{"Id":14,"Name":"Indiana","Abbreviation":"IN","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"18","Assoc_press":"Ind.","Standard_federal_region":"V","Census_region":2,"Census_region_name":"Midwest","Census_division":3,"Census_division_name":"East North Central","Circuit_court":7},"KS":{"Id":16,"Name":"Kansas","Abbreviation":"KS","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"20","Assoc_press":"Kan.","Standard_federal_region":"VII","Census_region":2,"Census_region_name":"Midwest","Census_division":4,"Census_division_name":"West North Central","Circuit_court":10},"KY":{"Id":17,"Name":"Kentucky","Abbreviation":"KY","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"21","Assoc_press":"Ky.","Standard_federal_region":"IV","Census_region":3,"Census_region_name":"South","Census_division":6,"Census_division_name":"East South Central","Circuit_court":6},"LA":{"Id":18,"Name":"Louisiana","Abbreviation":"LA","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"22","Assoc_press":"La.","Standard_federal_region":"VI","Census_region":3,"Census_region_name":"South","Census_division":7,"Census_division_name":"West South Central","Circuit_court":5},"MA":{"Id":21,"Name":"Massachusetts","Abbreviation":"MA","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"25","Assoc_press":"Mass.","Standard_federal_region":"I","Census_region":1,"Census_region_name":"Northeast","Census_division":1,"Census_division_name":"New England","Circuit_court":1},"MD":{"Id":20,"Name":"Maryland","Abbreviation":"MD","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"24","Assoc_press":"Md.","Standard_federal_region":"III","Census_region":3,"Census_region_name":"South","Census_division":5,"Census_division_name":"South Atlantic","Circuit_court":4},"ME":{"Id":19,"Name":"Maine","Abbreviation":"ME","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"23","Assoc_press":"Maine","Standard_federal_region":"I","Census_region":1,"Census_region_name":"Northeast","Census_division":1,"Census_division_name":"New England","Circuit_court":1},"MI":{"Id":22,"Name":"Michigan","Abbreviation":"MI","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"26","Assoc_press":"Mich.","Standard_federal_region":"V","Census_region":2,"Census_region_name":"Midwest","Census_division":3,"Census_division_name":"East North Central","Circuit_court":6},"MN":{"Id":23,"Name":"Minnesota","Abbreviation":"MN","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"27","Assoc_press":"Minn.","Standard_federal_region":"V","Census_region":2,"Census_region_name":"Midwest","Census_division":4,"Census_division_name":"West North Central","Circuit_court":8},"MO":{"Id":25,"Name":"Missouri","Abbreviation":"MO","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"29","Assoc_press":"Mo.","Standard_federal_region":"VII","Census_region":2,"Census_region_name":"Midwest","Census_division":4,"Census_division_name":"West North Central","Circuit_court":8},"MS":{"Id":24,"Name":"Mississippi","Abbreviation":"MS","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"28","Assoc_press":"Miss.","Standard_federal_region":"IV","Census_region":3,"Census_region_name":"South","Census_division":6,"Census_division_name":"East South Central","Circuit_court":5},"MT":{"Id":26,"Name":"Montana","Abbreviation":"MT","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"30","Assoc_press":"Mont.","Standard_federal_region":"VIII","Census_region":4,"Census_region_name":"West","Census_division":8,"Census_division_name":"Mountain","Circuit_court":9},"NC":{"Id":33,"Name":"North Carolina","Abbreviation":"NC","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"37","Assoc_press":"N.C.","Standard_federal_region":"IV","Census_region":3,"Census_region_name":"South","Census_division":5,"Census_division_name":"South Atlantic","Circuit_court":4},"ND":{"Id":34,"Name":"North Dakota","Abbreviation":"ND","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"38","Assoc_press":"N.D.","Standard_federal_region":"VIII","Census_region":2,"Census_region_name":"Midwest","Census_division":4,"Census_division_name":"West North Central","Circuit_court":8},"NE":{"Id":27,"Name":"Nebraska","Abbreviation":"NE","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"31","Assoc_press":"Neb.","Standard_federal_region":"VII","Census_region":2,"Census_region_name":"Midwest","Census_division":4,"Census_division_name":"West North Central","Circuit_court":8},"NH":{"Id":29,"Name":"New Hampshire","Abbreviation":"NH","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"33","Assoc_press":"N.H.","Standard_federal_region":"I","Census_region":1,"Census_region_name":"Northeast","Census_division":1,"Census_division_name":"New England","Circuit_court":1},"NJ":{"Id":30,"Name":"New Jersey","Abbreviation":"NJ","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"34","Assoc_press":"N.J.","Standard_federal_region":"II","Census_region":1,"Census_region_name":"Northeast","Census_division":2,"Census_division_name":"Mid-Atlantic","Circuit_court":3},"NM":{"Id":31,"Name":"New Mexico","Abbreviation":"NM","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"35","Assoc_press":"N.M.","Standard_federal_region":"VI","Census_region":4,"Census_region_name":"West","Census_division":8,"Census_division_name":"Mountain","Circuit_court":10},"NV":{"Id":28,"Name":"Nevada","Abbreviation":"NV","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"32","Assoc_press":"Nev.","Standard_federal_region":"IX","Census_region":4,"Census_region_name":"West","Census_division":8,"Census_division_name":"Mountain","Circuit_court":9},"NY":{"Id":32,"Name":"New York","Abbreviation":"NY","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"36","Assoc_press":"N.Y.","Standard_federal_region":"II","Census_region":1,"Census_region_name":"Northeast","Census_division":2,"Census_division_name":"Mid-Atlantic","Circuit_court":2},"OH":{"Id":35,"Name":"Ohio","Abbreviation":"OH","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"39","Assoc_press":"Ohio","Standard_federal_region":"V","Census_region":2,"Census_region_name":"Midwest","Census_division":3,"Census_division_name":"East North Central","Circuit_court":6},"OK":{"Id":36,"Name":"Oklahoma","Abbreviation":"OK","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"40","Assoc_press":"Okla.","Standard_federal_region":"VI","Census_region":3,"Census_region_name":"South","Census_division":7,"Census_division_name":"West South Central","Circuit_court":10},"OR":{"Id":37,"Name":"Oregon","Abbreviation":"OR","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"41","Assoc_press":"Ore.","Standard_federal_region":"X","Census_region":4,"Census_region_name":"West","Census_division":9,"Census_division_name":"Pacific","Circuit_court":9},"PA":{"Id":38,"Name":"Pennsylvania","Abbreviation":"PA","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"42","Assoc_press":"Pa.","Standard_federal_region":"III","Census_region":1,"Census_region_name":"Northeast","Census_division":2,"Census_division_name":"Mid-Atlantic","Circuit_court":3},"RI":{"Id":39,"Name":"Rhode Island","Abbreviation":"RI","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"44","Assoc_press":"R.I.","Standard_federal_region":"I","Census_region":1,"Census_region_name":"Northeast","Census_division":1,"Census_division_name":"New England","Circuit_court":1},"SC":{"Id":40,"Name":"South Carolina","Abbreviation":"SC","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"45","Assoc_press":"S.C.","Standard_federal_region":"IV","Census_region":3,"Census_region_name":"South","Census_division":5,"Census_division_name":"South Atlantic","Circuit_court":4},"SD":{"Id":41,"Name":"South Dakota","Abbreviation":"SD","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"46","Assoc_press":"S.D.","Standard_federal_region":"VIII","Census_region":2,"Census_region_name":"Midwest","Census_division":4,"Census_division_name":"West North Central","Circuit_court":8},"TN":{"Id":42,"Name":"Tennessee","Abbreviation":"TN","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"47","Assoc_press":"Tenn.","Standard_federal_region":"IV","Census_region":3,"Census_region_name":"South","Census_division":6,"Census_division_name":"East South Central","Circuit_court":6},"TX":{"Id":43,"Name":"Texas","Abbreviation":"TX","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"48","Assoc_press":"Texas","Standard_federal_region":"VI","Census_region":3,"Census_region_name":"South","Census_division":7,"Census_division_name":"West South Central","Circuit_court":5},"UT":{"Id":44,"Name":"Utah","Abbreviation":"UT","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"49","Assoc_press":"Utah","Standard_federal_region":"VIII","Census_region":4,"Census_region_name":"West","Census_division":8,"Census_division_name":"Mountain","Circuit_court":10},"VA":{"Id":46,"Name":"Virginia","Abbreviation":"VA","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"51","Assoc_press":"Va.","Standard_federal_region":"III","Census_region":3,"Census_region_name":"South","Census_division":5,"Census_division_name":"South Atlantic","Circuit_court":4},"VT":{"Id":45,"Name":"Vermont","Abbreviation":"VT","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"50","Assoc_press":"Vt.","Standard_federal_region":"I","Census_region":1,"Census_region_name":"Northeast","Census_division":1,"Census_division_name":"New England","Circuit_court":2},"WA":{"Id":47,"Name":"Washington","Abbreviation":"WA","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"53","Assoc_press":"Wash.","Standard_federal_region":"X","Census_region":4,"Census_region_name":"West","Census_division":9,"Census_division_name":"Pacific","Circuit_court":9},"WI":{"Id":49,"Name":"Wisconsin","Abbreviation":"WI","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"55","Assoc_press":"Wis.","Standard_federal_region":"V","Census_region":2,"Census_region_name":"Midwest","Census_division":3,"Census_division_name":"East North Central","Circuit_court":7},"WV":{"Id":48,"Name":"West Virginia","Abbreviation":"WV","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"54","Assoc_press":"W.Va.","Standard_federal_region":"III","Census_region":3,"Census_region_name":"South","Census_division":5,"Census_division_name":"South Atlantic","Circuit_court":4},"WY":{"Id":50,"Name":"Wyoming","Abbreviation":"WY","Country":"USA","Type_state":"state","Sort":10,"Status":"current","Occupied":"occupied","Notes":"","Fips_state":"56","Assoc_press":"Wyo.","Standard_federal_region":"VIII","Census_region":4,"Census_region_name":"West","Census_division":8,"Census_division_name":"Mountain","Circuit_court":10}} diff --git a/statecsv2json/main.go b/statecsv2json/main.go new file mode 100644 index 0000000..c4634b2 --- /dev/null +++ b/statecsv2json/main.go @@ -0,0 +1,157 @@ +package main + +import "encoding/csv" +import "encoding/json" +import "io" +import "log" +import "os" +import "strconv" + +type StateData struct { + Id int + Name string + Abbreviation string + Country string + Type_state string + Sort int + Status string + Occupied string + Notes string + Fips_state string + Assoc_press string + Standard_federal_region string + Census_region int + Census_region_name string + Census_division int + Census_division_name string + Circuit_court int +} + +func readCsvHeader(line []string) (map[string]int, error) { + columns := make(map[string]int) + for i, col := range line { + columns[col] = i + } + return columns, nil +} + +func readCsvLine(line []string, columns map[string]int) (*StateData, error) { + var err error + data := new(StateData) + for i, col := range line { + switch i { + case columns["id"]: + data.Id, err = strconv.Atoi(col) + if err != nil { + return data, err + } + case columns["name"]: + data.Name = col + case columns["abbreviation"]: + data.Abbreviation = col + case columns["country"]: + data.Country = col + case columns["type"]: + data.Type_state = col + case columns["sort"]: + data.Sort, err = strconv.Atoi(col) + if err != nil { + return data, err + } + case columns["status"]: + data.Status = col + case columns["occupied"]: + data.Occupied = col + case columns["notes"]: + data.Notes = col + case columns["fips_state"]: + data.Fips_state = col + case columns["assoc_press"]: + data.Assoc_press = col + case columns["standard_federal_region"]: + data.Standard_federal_region = col + case columns["census_region"]: + data.Census_region, err = strconv.Atoi(col) + if err != nil { + return data, err + } + case columns["census_region_name"]: + data.Census_region_name = col + case columns["census_division"]: + data.Census_division, err = strconv.Atoi(col) + if err != nil { + return data, err + } + case columns["census_division_name"]: + data.Census_division_name = col + case columns["circuit_court"]: + data.Circuit_court, err = strconv.Atoi(col) + if err != nil { + return data, err + } + } + } + return data, nil +} + +func readCsv(in io.Reader) map[string]*StateData { + data := make(map[string]*StateData) + csvReader := csv.NewReader(in) + var header map[string]int + for { + line, err := csvReader.Read() + if err == io.EOF { + break + } else if err != nil { + log.Fatal(err) + } + if len(header) == 0 { + header, err = readCsvHeader(line) + if err != nil { + log.Fatal(err) + } + } else { + d, err := readCsvLine(line, header) + if err != nil { + log.Println("Invalid line", err) // skip + } else { + data[d.Abbreviation] = d + } + } + } + return data +} + +func main() { + if len(os.Args) < 2 { + log.Fatalln("Usage statecsv ") + } + + // read input + csv, err := os.Open(os.Args[1]) + if err != nil { + log.Fatalln("Error reading file", err) + } + defer csv.Close() + + // parse data + data := readCsv(csv) + + // convert JSON + var writer io.Writer + if len(os.Args) > 2 { + f, ferr := os.Create(os.Args[2]) + if ferr != nil { + panic(ferr) + } + defer f.Close() + writer = f + } else { + writer = os.Stdout + } + err = json.NewEncoder(writer).Encode(data) + if err != nil { + panic(err) + } + +}