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)
| 89 | // to the official CSV standard. |
| 90 | // Reads all values and assigns as many as fit. |
| 91 | func 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 | } |
| 114 | done: |
| 115 | return nil |
| 116 | } |
no test coverage detected