Skip to content

encoding/csv: allow trailing commas always #5892

@mholt

Description

@mholt
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions