MCPcopy Create free account
hub / github.com/cogentcore/core / ReadCSV

Function ReadCSV

tensor/io.go:91–116  ·  view source on GitHub ↗

ReadCSV reads a tensor from a comma-separated-values (CSV) file (where comma = any delimiter, specified in the delim arg), using the Go standard encoding/csv reader conforming to the official CSV standard. Reads all values and assigns as many as fit.

(tsr Tensor, r io.Reader, delim rune)

Source from the content-addressed store, hash-verified

89// to the official CSV standard.
90// Reads all values and assigns as many as fit.
91func ReadCSV(tsr Tensor, r io.Reader, delim rune) error {
92 cr := csv.NewReader(r)
93 if delim != 0 {
94 cr.Comma = delim
95 }
96 rec, err := cr.ReadAll() // todo: lazy, avoid resizing
97 if err != nil || len(rec) == 0 {
98 return err
99 }
100 rows := len(rec)
101 cols := len(rec[0])
102 sz := tsr.Len()
103 idx := 0
104 for ri := 0; ri < rows; ri++ {
105 for ci := 0; ci < cols; ci++ {
106 str := rec[ri][ci]
107 tsr.SetString1D(idx, str)
108 idx++
109 if idx >= sz {
110 goto done
111 }
112 }
113 }
114done:
115 return nil
116}

Callers 1

OpenCSVFunction · 0.85

Calls 2

LenMethod · 0.65
SetString1DMethod · 0.65

Tested by

no test coverage detected