csv

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2019 License: BSD-3-Clause Imports: 3 Imported by: 13

README

go-whosonfirst-csv

A simple 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.

Install

You will need to have both Go (specifically version 1.12 or higher because we're using Go modules) and the make programs installed on your computer. Assuming you do just type:

make tools

Usage

Reading files
import (
	csv "github.com/whosonfirst/go-whosonfirst-csv"
	"os"
)

reader, reader_err := csv.NewDictReaderFromPath("example.csv")

// or maybe you might do
// reader, err := csv.NewDictReader(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 (
	csv "github.com/whosonfirst/go-whosonfirst-csv"
	"os"
)

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

writer, err := csv.NewDictWriter(os.Stdout, fieldnames)

// or maybe you might do
// writer, err := csv.NewDictWriterFromPath("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)

Tools

wof-csv-filter

Concatenate one or more CSV files ensuring a common set of columns.

./bin/wof-csv-filter -h
Usage of wof-csv-filter:
  $> wof-csv-filter -options <files>

Valid options are:
  -columns string
    	   Columns to filter on. A value of "-" means the set of unique columns for all CSV files being filtered. (default "-")
  -out string
       Where to write the data. A value of "-" means write the data to STDOUT. (default "-")

See also

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DictReader

type DictReader struct {
	Reader     *gocsv.Reader
	Fieldnames []string
}

func NewDictReader

func NewDictReader(fh io.Reader) (*DictReader, error)

func NewDictReaderFromPath

func NewDictReaderFromPath(path string) (*DictReader, error)

func (DictReader) Read

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

type DictWriter

type DictWriter struct {
	Writer     *gocsv.Writer
	Fieldnames []string
}

func NewDictWriter

func NewDictWriter(fh io.Writer, fieldnames []string) (*DictWriter, error)

func NewDictWriterFromPath

func NewDictWriterFromPath(path string, fieldnames []string) (*DictWriter, error)

func (DictWriter) WriteHeader

func (dw DictWriter) WriteHeader()

func (DictWriter) WriteRow

func (dw DictWriter) WriteRow(row map[string]string)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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