Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/csv: allow trailing commas always #5892

Closed
mholt opened this issue Jul 15, 2013 · 8 comments
Closed

encoding/csv: allow trailing commas always #5892

mholt opened this issue Jul 15, 2013 · 8 comments
Milestone

Comments

@mholt
Copy link

mholt commented Jul 15, 2013

What steps will reproduce the problem?
1. Put this perfectly good CSV file somewhere:

locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode
1,O1,,,,0.0000,0.0000,,
2,AP,,,,35.0000,105.0000,,
3,EU,,,,47.0000,8.0000,,
4,AD,,,,42.5000,1.5000,,
5,AE,,,,24.0000,54.0000,,
6,AF,,,,33.0000,65.0000,,
7,AG,,,,17.0500,-61.8000,,
8,AI,,,,18.2500,-63.1667,,
9,AL,,,,41.0000,20.0000,,
10,AM,,,,40.0000,45.0000,,


2. Use code like this to read it:

import "encoding/csv"
import "os"

csvFile, err := os.Open("/path/to/csv/file.csv")
defer csvFile.Close()

if err != nil {
    panic(err)
}

csvf := csv.NewReader(csvFile)
csvf.Read()     // skip header row


for {
    fields, err := csvf.Read()
    if err == io.EOF {
        break
    } else if err != nil {
        panic(err) // this is where the panic happens
    }
}


3. Go run it

What is the expected output?
No panic


What do you see instead?


panic: line 2, column 23: extra delimiter at end of line

goroutine 1 [running]:
main.main()
    /path/to/program.go:239 +0x107e

goroutine 2 [runnable]:
exit status 2



Which compiler are you using (5g, 6g, 8g, gccgo)?
8g


Which operating system are you using?
Mac OS 10.8

Which version are you using?  (run 'go version')
go1.1 darwin/amd64

Please provide any additional information below.

Putting a value in the last field so that it's non-empty fixes it. But the field is
empty, and that should be OK.
@rsc
Copy link
Contributor

rsc commented Jul 15, 2013

Comment 1:

Set r.TrailingComma = true in the reader.

Status changed to WorkingAsIntended.

@mholt
Copy link
Author

mholt commented Jul 16, 2013

Comment 2:

Unfortunate and disappointing that is not the default behavior.

@rsc
Copy link
Contributor

rsc commented Aug 1, 2013

Comment 3:

I checked into why the package does this, and I think it is a misunderstanding of a
sentence in RFC 4180. I believe we should always allow trailing commas.
https://golang.org/cl/11916045 has a start, if anyone wants to pick it up. The
only problem is that the TrailingCommaSpaceEOF test is not getting the answer I think it
should, perhaps a bug that has been hidden by the comma paranoia.

Labels changed: added priority-later, go1.2, removed priority-triage.

Status changed to Accepted.

@PieterD
Copy link
Contributor

PieterD commented Aug 1, 2013

Comment 4:

The TrailingComma you removed had a TrimLeadingSpace in it, which checked for EOF.
With it gone, it was down to TrimLeadingSpace in parseField(), which did not check for
EOF.
I'll mail in a second.

@PieterD
Copy link
Contributor

PieterD commented Aug 1, 2013

Comment 5:

I don't know what the procedure is. I can't upload or mail the CL because I don't own
it, so to be safe I'll just make a new one. If that's not the way to go, feel free to
berate me.

@PieterD
Copy link
Contributor

PieterD commented Aug 1, 2013

Comment 6:

https://golang.org/cl/12294043/
Again, I'm sorry if this isn't the way to do handle picking up someone else's CL, but I
felt I had to send this out before going to sleep.

@mholt
Copy link
Author

mholt commented Aug 7, 2013

Comment 7:

I'm glad this is being reviewed, and possibly changed. In hindsight, I'll just comment
that I find it very odd that Go REQUIRES trailing commas when defining map and struct
literals but FORBIDS them here, unless you explicitly allow it. Seems inconsistent for
an engineered language.

@robpike
Copy link
Contributor

robpike commented Aug 9, 2013

Comment 8:

This issue was closed by revision f2bc275.

Status changed to Fixed.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants