csvutil

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2020 License: MIT Imports: 4 Imported by: 0

README

csvutil

GoDoc Go Report Card Action

Installation

go get -u github.com/sj14/csvutil

Examples

For all options please check the godoc. The examples ignore all error handling!


Create a new dataset

All datasets should contain a header for further processing.

records := [][]string{
    {"first_name", "last_name", "username"},
    {"Rob", "Pike", "rob"},
    {"Ken", "Thompson", "ken"},
    {"Robert", "Griesemer", "gri"},
}

ds := csvutil.New(records)
Add a new row
ds.AddRow([]string{"my first name", "my last name", "my nick"})
first_name,last_name,username
Rob,Pike,rob
Ken,Thompson,ken
Robert,Griesemer,gri
my first name,my last name,my nick
Add multiple rows
ds.AddRows([][]string{
    {"my first name 0", "my last name 0", "my nick 0"},
    {"my first name 1", "my last name 1", "my nick 1"},
    {"my first name 2", "my last name 2", "my nick 2"},
})
first_name,last_name,username
Rob,Pike,rob
Ken,Thompson,ken
Robert,Griesemer,gri
my first name 0,my last name 0,my nick 0
my first name 1,my last name 1,my nick 1
my first name 2,my last name 2,my nick 2
Add a new column at index 1
ds.AddCol([]string{"column_headline", "my row 1", "my row 2", "my row 3"}, 1)
ds.Write(os.Stdout)
first_name,column_headline,last_name,username
Rob,my row 1,Pike,rob
Ken,my row 2,Thompson,ken
Robert,my row 3,Griesemer,gri
Get Column
lastNames, _ := ds.GetCol("last_name")
fmt.Println(lastNames)
[last_name Pike Thompson Griesemer]
Rename Column
ds.RenameCol("username", "nick")
first_name,last_name,nick
Rob,Pike,rob
Ken,Thompson,ken
Robert,Griesemer,gri
Move Column
ds.MoveCol("first_name", 1)
last_name,first_name,nick
Pike,Rob,rob
Thompson,Ken,ken
Griesemer,Robert,gri
Delete Column
ds.DeleteCol("first_name")
last_name,nick
Pike,rob
Thompson,ken
Griesemer,gri
Modify Column
addRowNumber := func(val string, i int) string { return fmt.Sprintf("%v (%v)", val, i) }
ds.ModifyCol("first_name", addRowNumber)
ds.Write(os.Stdout)
first_name,last_name,username
Rob (1),Pike,rob
Ken (2),Thompson,ken
Robert (3),Griesemer,gri
Write the dataset
ds.Write(os.Stdout)
first_name,last_name,username
Rob,Pike,rob
Ken,Thompson,ken
Robert,Griesemer,gri
Write options
ds.Write(os.Stdout, csvutil.Delimiter('|'), csvutil.UseCLRF(true))
first_name|last_name|username
Rob|Pike|rob
Ken|Thompson|ken
Robert|Griesemer|gri
Get dataset as [][]string
fmt.Println(ds.Raw())
[[first_name last_name username] [Rob Pike rob] [Ken Thompson ken] [Robert Griesemer gri]]
Compare two (raw) datasets
a := [][]string{
    {"a", "b", "c"},
    {"d", "e", "f"},
}

b := [][]string{
    {"g", "h", "i"},
    {"j", "k", "l"},
}

result := csvutil.Equals(a, b)
fmt.Println(result)
false

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrColNotFound is returned when a named column is not found.
	ErrColNotFound = errors.New("column not found")
	// ErrSameColName is returned when a named column exists multiple times.
	ErrSameColName = errors.New("multiple columns with same name")
	// ErrColLen is returned when the length of a column to add differs from the already existing dataset column length.
	ErrColLen = errors.New("length of columns differ")
)

Functions

func Delimiter

func Delimiter(delimiter rune) option

Delimiter is the separator between each value (default: ',').

func Equals

func Equals(datasetA, datasetB [][]string) bool

Equals checks if both datasets are the same.

func UseCLRF

func UseCLRF(useCLRF bool) option

UseCLRF is set to false by default. If set to true, the Writer ends each line with \r\n instead of \n.

Types

type Dataset

type Dataset struct {
	// contains filtered or unexported fields
}

Dataset contains the CSV content and eases modificating it.

func New

func New(dataset [][]string) Dataset

New creates a new CSV dataset.

func (*Dataset) AddCol

func (ds *Dataset) AddCol(column []string, index int) error

AddCol inserts the given column at the position of the index. -1 adds the column at the last column -2 adds the column as the second last column, and so on...

func (*Dataset) AddRow

func (ds *Dataset) AddRow(row []string) error

AddRow appends the given row to the dataset.

func (*Dataset) AddRows

func (ds *Dataset) AddRows(rows [][]string) error

AddRows appends the given rows to the dataset.

func (*Dataset) DeleteCol

func (ds *Dataset) DeleteCol(name string) error

DeleteCol deletes the column with the given name.

func (*Dataset) GetCol added in v0.0.2

func (ds *Dataset) GetCol(name string) ([]string, error)

GetCol returns the column with the given name.

func (*Dataset) ModifyCol

func (ds *Dataset) ModifyCol(name string, f func(val string, row int) string) error

ModifyCol changes the values of column 'name' according to 'f'. 'val' contains the column value and 'row' is the current row number.

func (*Dataset) MoveCol added in v0.0.2

func (ds *Dataset) MoveCol(name string, index int) error

MoveCol moves the given column to index.

func (*Dataset) Raw

func (ds *Dataset) Raw() [][]string

Raw returns the raw data usable with Go's stdlib csv package.

func (*Dataset) RenameCol

func (ds *Dataset) RenameCol(old, new string) error

RenameCol renames the header of column 'old' to 'new'.

func (*Dataset) Write

func (ds *Dataset) Write(writer io.Writer, opts ...option) error

Write the dataset to the given writer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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