Skip to content

Go package to implement a "dict reader" style CSV parser (on top of the default encoding/csv package) to return rows a key-value dictionaries rather than lists.

License

Notifications You must be signed in to change notification settings

sfomuseum/go-csvdict

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-csvdict

Go package to implement a "dict reader" style CSV parser (on top of the default encoding/csv package) to return rows a key-value dictionaries rather than lists.

Documentation

Go Reference

Example

Reading files

import (
	"os"

        "github.com/whosonfirst/go-csvdict/v2"
)

r, _ := csvdict.NewReaderFromPath("example.csv")

// or maybe you might do
// r, _ := csvdict.NewReader(os.Stdin)

for {
	row, err := r.Read()

	if err == io.EOF {
		break
	}

	if err != nil {
		return err
	}

	value, ok := row["some-key"]
	// and so on...
}

It is also possible to iterate through all the records using the Iterate method:

import (
	"os"

        "github.com/whosonfirst/go-csvdict/v2"
)

r, _ := csvdict.NewReaderFromPath("example.csv")

// or maybe you might do
// r, _ := csvdict.NewReader(os.Stdin)

for row, err := r.Iterate() {

	if err != nil {
		return err
	}

	value, ok := row["some-key"]
	// and so on...
}

Writing files

import (
	"os"

	"github.com/whosonfirst/go-csvdict/v2"
)

wr, _ := csvdict.NewWriter(os.Stdout)

// or maybe you might do
// wrr, _ := csvdict.NewWriterFromPath("new.csv")

row := make(map[string]{
	"foo": "hello",
	"bar": "world",
}

wr.WriteRow(row)

See also

About

Go package to implement a "dict reader" style CSV parser (on top of the default encoding/csv package) to return rows a key-value dictionaries rather than lists.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages