State CSV

This commit is contained in:
Meutel 2017-07-07 19:28:15 +02:00
parent 22c69aaf27
commit 994ca5ddf2
2 changed files with 176 additions and 0 deletions

1
.gitignore vendored
View File

@ -32,3 +32,4 @@ go-cat2/go-cat2
lower-1line/lower-1line lower-1line/lower-1line
upper-words/upper-words upper-words/upper-words
longest-word/longest-word longest-word/longest-word
statecsv/statecsv

175
statecsv/main.go Normal file
View File

@ -0,0 +1,175 @@
package main
import "encoding/csv"
import "fmt"
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 printStateHtml(state *StateData) {
fmt.Println(" <tr><td>" + state.abbreviation + "</td><td>" + state.name + "</td></tr>")
}
func printHeader() {
fmt.Println(`<!DOCTYPE html>
<html>
<head><title>State</title></head>
<body>
<table>
<tr><th>Abbrev.</th><th>Name</th>`)
}
func printFooter() {
fmt.Println(` </table>
</body>
</html> `)
}
func main() {
if len(os.Args) < 2 {
log.Fatalln("Usage statecsv <file>")
}
// 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)
// display as HTML
printHeader()
if len(os.Args) > 2 {
for _, st := range os.Args[2:] {
state := data[st]
if state == nil {
log.Fatalln("Unkown state")
}
printStateHtml(state)
}
} else {
for _, state := range data {
printStateHtml(state)
}
}
printFooter()
}