Handling non-standard data

Parsing the data is a piece of cake with only the standard library:

func parse(l loader) (dates []string, co2s []float64) {  s := bufio.NewScanner(l())  for s.Scan() {    row := s.Text()    if strings.HasPrefix(row, "#") {      continue    }    fields := strings.Fields(row)    dates = append(dates, fields[2])    co2, err := strconv.ParseFloat(fields[4], 64)    dieIfErr(err)    co2s = append(co2s, co2)  }  return}

The parsing function takes a loader, which when called, returns a io.Reader. We then wrap the io.Reader in a bufio.Scanner. Recall that the format is not standard. There are some things that we want and some things we don't. The data however is in a fairly consistent format—we can use the standard library functions to filter the ones we ...

Get Go Machine Learning Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.