Reading CSV file



examples/read-csv/process_csv_file.csv
Tudor;Vidor;10;Hapci
Szundi;Morgo;7;Szende
Kuka;Hofeherke;100;Kiralyno
Boszorkany;Herceg;9;Meselo


examples/read-csv/read_csv.go
package main

import (
    "bufio"
    "encoding/csv"
    "fmt"
    "log"
    "os"
    "strconv"
)

func main() {
    if len(os.Args) != 2 {
        fmt.Printf("Usage: %s FILENAME\n", os.Args[0])
        os.Exit(1)
    }
    var filename = os.Args[1]
    //fmt.Println(filename)
    fh, err := os.Open(filename)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    reader := bufio.NewReader(fh)
    r := csv.NewReader(reader)
    r.Comma = ';'

    records, err := r.ReadAll()
    if err != nil {
        log.Fatal(err)
    }

    //fmt.Printf("%T\n", records)
    var sum = 0
    for _, row := range records {
        val, err := strconv.Atoi(row[2])
        if err != nil {
            fmt.Printf("Error %v converting to int: '%v'", err, row[2])
            continue
        }
        sum += val
    }
    fmt.Println(sum)
}

go run examples/read-csv/read_csv.go examples/csv/process_csv_file.csv

Result:


126