-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.go
51 lines (41 loc) · 855 Bytes
/
csv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package mathlib
import (
"encoding/csv"
"os"
"strconv"
)
// ReadCSV reads all the data from csv file
func ReadCSV(filename string) ([][]string, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
// f is a Reader
reader := csv.NewReader(f)
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
return records, nil
}
// ReadCSVAsFloat returns the values of csv as floats
// TODO runtime for very large csv file ???
func ReadCSVAsFloat(filename string) ([][]float64, error) {
var data [][]float64
records, err := ReadCSV(filename)
if err != nil {
return nil, err
}
for _, r := range records {
var f []float64
for _, v := range r {
fl, err := strconv.ParseFloat(v, 64)
if err != nil {
return nil, err
}
f = append(f, fl)
}
data = append(data, f)
}
return data, nil
}