csvdict

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2021 License: BSD-3-Clause Imports: 4 Imported by: 22

README

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 (
        "github.com/whosonfirst/go-csvdict"
	"os"
)

reader, reader_err := csvdict.NewReaderFromPath("example.csv")

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

if err != nil {
	panic(err)
}

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

	if err == io.EOF {
		break
	}

	if err != nil {
		return err
	}

	value, ok := row["some-key"]

	// and so on...
}
Writing files
import (
	"github.com/whosonfirst/go-csvdict"
	"os"
)

fieldnames := []string{"foo", "bar"}

writer, err := csvdict.NewWriter(os.Stdout, fieldnames)

// or maybe you might do
// writer, err := csvdict.NewWriterFromPath("new.csv", fieldnames)

if err != nil {
	panic(err)
}

writer.WriteHeader()

row := make(map[string]string)
row["foo"] = "hello"
row["bar"] = "world"

// See this? "baz" is not included in the list of fieldnames
// above so it will be silently ignored and excluded from your
// CSV file. Perhaps it should trigger an error. It doesn't, today...

row["baz"] = "wub wub wub"

writer.WriteRow(row)

See also

Documentation

Overview

package csvdict implements a "dictionary reader" style CSV parser (on top of the default `encoding/csv` package) to return rows a key-value dictionaries rather than lists. Example

Reading files

import (
        "github.com/whosonfirst/go-csvdict"
	"os"
)

reader, reader_err := csvdict.NewReaderFromPath("example.csv")

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

if err != nil {
	panic(err)
}

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

	if err == io.EOF {
		break
	}

	if err != nil {
		return err
	}

	value, ok := row["some-key"]

	// and so on...
}

Writing files

import (
	"github.com/whosonfirst/go-csvdict"
	"os"
)

fieldnames := []string{"foo", "bar"}

writer, err := csvdict.NewWriter(os.Stdout, fieldnames)

// or maybe you might do
// writer, err := csvdict.NewWriterFromPath("new.csv", fieldnames)

if err != nil {
	panic(err)
}

writer.WriteHeader()

row := make(map[string]string)
row["foo"] = "hello"
row["bar"] = "world"

// See this? "baz" is not included in the list of fieldnames
// above so it will be silently ignored and excluded from your
// CSV file. Perhaps it should trigger an error. It doesn't, today...

row["baz"] = "wub wub wub"

writer.WriteRow(row)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	Reader     *csv.Reader
	Fieldnames []string
}

type Reader implements a `encoding/csv` style reader for CSV documents with named columns.

func NewReader

func NewReader(r io.Reader) (*Reader, error)

NewReader will return a Reader instance that will load data from 'r'

func NewReaderFromPath

func NewReaderFromPath(path string) (*Reader, error)

NewReader will return a Reader instance that will load data from 'path'

func (Reader) Read

func (dr Reader) Read() (map[string]string, error)

Read reads one record (a slice of fields) from r and returns a map[string]string mapping columns to their corresponding names, as defined in the first line of r.

type Writer

type Writer struct {
	Writer     *csv.Writer
	Fieldnames []string
}

type Writer implements a `encoding/csv` style writer for CSV documents with named columns.

func NewWriter

func NewWriter(wr io.Writer, fieldnames []string) (*Writer, error)

NewWriter will return a new Writer that writes to wr using a set list of column names defined in fieldnames.

func NewWriterFromPath

func NewWriterFromPath(path string, fieldnames []string) (*Writer, error)

NewWriter will return a new Writer that writes to path using a set list of column names defined in fieldnames.

func (Writer) Error

func (dw Writer) Error() error

Error reports any error that has occurred during a previous Write or Flush.

func (Writer) Flush

func (dw Writer) Flush() error

Flush writes any buffered data to the underlying writer. To check if an error occurred during the Flush, call Error.

func (Writer) WriteHeader

func (dw Writer) WriteHeader() error

WriteHeader will write the CSV-encoded list of fieldnames passed to dw.

func (Writer) WriteRow

func (dw Writer) WriteRow(row map[string]string) error

WriteRow writes the values of row as CSV-encoded data. The order of those values is determined by their position defined in the list of fieldnames passed to dw.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL