UPDATE: This appears to have been fixed and the fix ships with Go 1.2.
I like Go (1.1.1), but how disappointing that in order to read this:
1,O1,,,,0.0000,0.0000,,
2,AP,,,,35.0000,105.0000,,
3,EU,,,,47.0000,8.0000,,
4,AD,,,,42.5000,1.5000,,
I need to tell this code:
csvFile, err := os.Open("/path/myfile.csv")
defer csvFile.Close()
if err != nil {
panic(err)
}
csvReader := csv.NewReader(csvFile)
for {
fields, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
// ... do stuff ...
}
to allow the last field in any row to be empty:
csvReader.TrailingComma = true
Otherwise:
panic: line 1, column 22: extra delimiter at end of line
Really??
One word: BIZARRE.
Even though my dissatisfaction with Go is quite low, this is probably my biggest disappointment so far. It's an obscure, barely-documented
// We don't allow trailing commas.So by trailing comma, they really mean, empty last fields. Um. Why.Doesn't Go REQUIRE trailing commas when defining map and struct literals? Why forbid them here? Kind of odd considering it's an engineered language.
A line break or comma in the data would enclose the field in quotes... so a comma at the end of a line must mean that the last field is... empty. That is all.
O little csv.Reader, your name is Reader, not SyntaxChecker. Leave it to the file's author to fix those flaws. But please, just read this properly-formatted CSV file in all its little glory. That's all I ask of you, csv.Reader... that's all I ask...
0 comments:
Post a Comment