For my CSV files, every row has the same numbers of columns, except for the last row, which was only one column. So, when I read the file data with "foreach" for getting total number of rows, I get the error that it has the wrong number of fields. How do I fix this error without deleting the last row? The code and CSV file are shown below.
openfile, err := os.Open(filepath)
checkError("Error in reading the file\n", err)
fmt.Println("Already open filepath :", filepath)
//read the data of file
filedata, err := csv.NewReader(openfile).ReadAll()
checkError("Error in reading the file\n", err)
leg := len(filedata)
fmt.Println("total no of rows:", leg)
close := make([]string, leg)
date := make([]string, leg)
open := make([]string, leg)
high := make([]string, leg)
low := make([]string, leg)
adjustclose := make([]string, leg)
volume := make([]string, leg)
for e, value := range filedata {
date[e] = value[0]
open[e] = value[1]
high[e] = value[2]
low[e] = value[3]
close[e] = value[4]
adjustclose[e] = value[5]
volume[e] = value[6]
}
2020-03-24,21,21,21,21,21,5
2020-04-06,20.8,20.8,20.8,20.8,20.8,19
2020-04-07,20.4,20.4,20.4,20.4,20.4,5
2020-04-09,20.4,20.4,20.4,20.4,20.4,10
292
go doc encoding/csv.Reader.FieldsPerRecord. Allways, literally allways read the full documentation of any package you use. In the case you read the documentation but could make sense of it: Use -1.