fastcsv

package module
v0.0.0-...-1e8f6d8 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2017 License: MIT Imports: 6 Imported by: 0

README

Fastcsv

Build Status

Fastcsv is a csv parser for golang.

Install

go get -t github.com/willwangcn/fastcsv

Usage

Parsing
  • sep="," If your data uses an alternate delimiter such as ; or \t.

  • header=false|true Set to true if you expect the first line of your CSV to contain headers, alternatly you can specify an array of headers using headers param. You can also specify a sparse array to omit some of the columns.

  • columns=slice|nil If your data doesn't contain header and want to specify the column name.

Notice headers column name must equals struct's field name ignoring case.

Example

type Student struct {
	Id       int64
	Name     string
	Age      int
	Lat, Lng float64
}
// right case, if you not config the field, it will not be initialized
headers = []string{"name", "age", "lat"}
headers = []string{"AGE", "NAme", "lng"}

// wrong case
headers = []string{"s_name", "s_age"}
Test
	data := bytes.NewBuffer([]byte(`
	Will,201601101716,18,40.654321,116.25820398331,2016-01-10 23:59:59
    Jack,201601101717,50,40.08296,116.316081,1990-01-31 23:59:59
    Tony,201601101718,44,40.060394,116.239552,1963-10-10 23:59:59`))
	columns := []string{"name", "id", "age", "lat", "lng", "birthday"}
	parser := fastcsv.NewFastcsv(data, ",", false, columns)
	students, err := parser.ReadAll(Student{})
	if err != nil {
		panic(err)
	}
	will := students[0].(Student)
	if will.Name != "Will" {
		t.Fail()
	}
How to Use
    f, err := os.Open(filePath)
    defer f.Close()
    if err != nil {
        return
    }
    columns := []string{"id", "name", "age", "lat", "lng"}
    parser, err := fastcsv.NewFastcsv(f, ",", false, headers)
	if err != nil {
		panic(err)
	}
    students := parser.ReadAll(Student{})
    for _, val := range students {
        stu := val.(Student)
        fmt.Println(stu)
    }

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fastcsv

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

func NewFastcsv

func NewFastcsv(reader io.Reader, sep string, header bool, headers []string) *Fastcsv

func (*Fastcsv) Err

func (parser *Fastcsv) Err() error

func (*Fastcsv) ReadAll

func (parser *Fastcsv) ReadAll(v interface{}) ([]interface{}, error)

func (*Fastcsv) ReadHeader

func (parser *Fastcsv) ReadHeader() map[string]int

Jump to

Keyboard shortcuts

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