func main() { filename := "./raw_subscribers.csv" f, err := os.Open(filename) if err != nil { fmt.Println("Failed to open " + filename) os.Exit(1) } defer f.Close() filename = "test.csv" outf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0644) if err != nil { fmt.Println(err) os.Exit(1) } defer outf.Close() c := csv.NewReader(f) c.TrailingComma = true outc := csv.NewWriter(outf) defer outc.Flush() // Grab CSV headers _, err = c.Read() exit_on_errors(err) for { row, err := c.Read() if err != nil { if err != os.EOF { fmt.Println(err) os.Exit(1) } else { break } } //layout, err := get_layout(row[0]) //fmt.Println("Using layout ==> " + layout) //t, _ := time.Parse(layout, row[0]) //fmt.Println(t) /* for i, field := range row { fmt.Printf(headers[i] + " => " + field + "\n") } */ if err := outc.Write(row); err != nil { fmt.Println(err) } } fmt.Println("Done. Good bye.\n") }
func OpenCsv(s string, mat *Matrix) { f, err := os.Open(s) if err != nil { fmt.Println("Could not Open the CSV File") return } read := csv.NewReader(io.Reader(f)) data, err := read.ReadAll() if err != nil { fmt.Println("Failed to read from the CSV File(Maybe the file does not comply to the CSV standard defined in RFC 4180)") } for i := 0; i < len(data); i++ { for j := 0; j < len(data[i]); j++ { ret, _ := strconv.Atoi(data[i][j]) mat.Data[i][j] = int(ret / 100) } } }
func OpenCsv(s string) { f, err := os.Open(s) if err != nil { fmt.Println("Could not Open the CSV File") return } read := csv.NewReader(io.Reader(f)) data, err := read.ReadAll() if err != nil { fmt.Println("Failed to read from the CSV File(Maybe the file does not comply to the CSV standard defined in RFC 4180)") } data0 := make([]int, len(data[0])*len(data)) for i := 0; i < len(data); i++ { for j := 0; j < len(data[i]); j++ { data0[(i*len(data[0]))+j], _ = strconv.Atoi(data[i][j]) } } fmt.Println(data0) }
func main() { f, err := os.Open("letters.txt") if err != nil { log.Print(err) os.Exit(-1) } reader := csv.NewReader(f) records, err := reader.ReadAll() if err != nil { log.Print(err) os.Exit(-1) } var ttl_letters, ttl_pts int for _, row := range records { freq, _ := strconv.Atoi(row[1]) pts, _ := strconv.Atoi(row[2]) ttl_letters += freq ttl_pts += (pts * freq) fmt.Printf("Letter %s has frequency %d and value %d\n", row[0], freq, pts) } fmt.Printf("Total letters: %d. Total points: %d\n", ttl_letters, ttl_pts) }