encoding/csvutil/one_col_list.go
package csvutil
import (
"fmt"
"math"
"os"
"github.com/grokify/mogo/type/stringsutil"
)
// ReadFileOneColListToGrid parses a file with one value per row.
func ReadFileOneColListToGrid(filename string, colCount int, validateLength bool) ([][]string, error) {
bytes, err := os.ReadFile(filename)
if err != nil {
return [][]string{}, err
}
return ParseOneColListToGrid(stringsutil.SplitLines(string(bytes)), colCount, validateLength)
}
// ParseOneColListToGrid parses a set of lines with one value per row.
func ParseOneColListToGrid(lines []string, colCount int, validateLength bool) ([][]string, error) {
rows := [][]string{}
curRow := []string{}
colCountFloat := float64(colCount)
for i, line := range lines {
curRow = append(curRow, line)
if math.Mod(float64(i+1), colCountFloat) == 0 {
if len(curRow) > 0 {
rows = append(rows, curRow)
curRow = []string{}
}
}
}
if len(curRow) > 0 {
rows = append(rows, curRow)
}
if validateLength {
for _, row := range rows {
if len(row) != colCount {
return rows, fmt.Errorf("error row length: want [%d] have [%d]", colCount, len(row))
}
}
}
return rows, nil
}