-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandling_unexpected_fields_1_2.go
111 lines (90 loc) · 2.53 KB
/
handling_unexpected_fields_1_2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strconv"
)
// CSVRecord1 contains a successfully parsed row of the CSV file.
type CSVRecord struct {
SepalLength float64
SepalWidth float64
PetalLength float64
PetalWidth float64
Species string
ParseError error
}
func main() {
// Open the iris dataset file.
f, err := os.Open("iris.csv")
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Create a new CSV reader reading from the opened file.
reader := csv.NewReader(f)
// We should have 5 fields per line. By setting
// FieldsPerRecord to 5, we can validate that each of the
// rows in our CSV has the correct number of fields.
reader.FieldsPerRecord = 5
// Create a slice value that will hold all of the successfully parsed
// records from the CSV.
var csvData []CSVRecord
// Read in the records looking for unexpected types.
for {
// Read in a row. Check if we are at the end of the file.
record, err := reader.Read()
if err == io.EOF {
break
}
// If we had a parsing error, log the error and move on.
if err != nil {
log.Println(err)
continue
}
// Create a CSVRecord1 value for the row.
var csvRecord CSVRecord
// Parse each of the values in the record based on an expected type.
for idx, value := range record {
// Parse the value in the record as a string for the string column.
if idx == 4 {
// Validate that the value is not an empty string. If the
// value is an empty string break the parsing loop.
if value == "" {
log.Printf("Unexpected type in column %d\n", idx)
csvRecord.ParseError = fmt.Errorf("Empty string value")
break
}
// Add the string value to the CSVRecord1.
csvRecord.Species = value
continue
}
// Otherwise, parse the value in the record as a float64.
var floatValue float64
// If the value can not be parsed as a float, log and break the
// parsing loop.
if floatValue, err = strconv.ParseFloat(value, 64); err != nil {
log.Printf("Unexpected type in column %d\n", idx)
csvRecord.ParseError = fmt.Errorf("Could not parse float")
break
}
// Add the float value to the respective field in the CSVRecord1.
switch idx {
case 0:
csvRecord.SepalLength = floatValue
case 1:
csvRecord.SepalWidth = floatValue
case 2:
csvRecord.PetalLength = floatValue
case 3:
csvRecord.PetalWidth = floatValue
}
}
// Append successfully parsed records to the slice defined above.
if csvRecord.ParseError == nil {
csvData = append(csvData, csvRecord)
}
}
}