CSV Package

178 views
Skip to first unread message

Mandolyte

unread,
Mar 10, 2011, 9:32:13 PM3/10/11
to golan...@googlegroups.com
I have been writing, and re-writing as I learn more, a package to parse CSV files.  It is based on http://tools.ietf.org/html/rfc4180 and the design is a finite state machine approach.  Perhaps a bit unusual is the choice of a map to model the results since it grows as needed.  I notice that there isn't a CSV parser in Go, so I thought I might contribute it.  Below are some design elements.  Comments?

type CSV struct {
rows map[int] (map[int] string)
rd io.Reader
DEBUG bool
}

Method set:
- func New(in io.Reader) (csv *CSV)
- func (c *CSV) Parse() (m map[int](map[int]string), error os.Error)
- func (m *CSV) String() string
- func (m *CSV) GetMap() map[int](map[int]string)

Todo:
- design writer support
- support macro ="..." style
- ponder CRLF

Andrew Gerrand

unread,
Mar 10, 2011, 9:47:46 PM3/10/11
to golan...@googlegroups.com, Mandolyte
This has come up before. The difficulty with CSV is that it is a
poorly-defined (or undefined) format. Writing a good, general-purpose
CSV parser is hard, so we've shied from including one in the standard
library.

You should make your code available in a form that is goinstallable[1]
and add it to the Go Dashboard[2], though.

Andrew

1. http://golang.org/cmd/goinstall/
2. http://godashboard.appspot.com/project

Russ Cox

unread,
Mar 10, 2011, 9:54:47 PM3/10/11
to golan...@googlegroups.com, Mandolyte
Using map[int]T instead of []T when the index
space is dense (0..n) is a mistake. For that case
the map is slower, larger, and has worse locality
than a slice. And you can't slice it.

Russ

chris dollin

unread,
Mar 11, 2011, 1:32:55 AM3/11/11
to golan...@googlegroups.com, Mandolyte
On 11 March 2011 02:32, Mandolyte <ceci...@gmail.com> wrote:
>  I notice that there
> isn't a CSV parser in Go, so I thought I might contribute it.

Also note

http://code.google.com/p/gocsv

(which I have used, a little)

Pacakges don't have to be in the Go tree to be available,

Chris

--
Chris "allusive" Dollin

Miki Tebeka

unread,
Mar 11, 2011, 1:33:55 PM3/11/11
to golan...@googlegroups.com
What about CSV headers? Can you provide a way to get a map where the keys are strings? (the headers)

Cecil New

unread,
Mar 13, 2011, 6:32:27 PM3/13/11
to golan...@googlegroups.com
Thanks to everyone's comments.  I have some refactoring to do based on the material I read in the Learning Go pdf and I'll also take a look at the other package.  
Reply all
Reply to author
Forward
0 new messages