tableschema-go: github.com/frictionlessdata/tableschema-go/csv Index | Examples | Files

package csv

import "github.com/frictionlessdata/tableschema-go/csv"

Index

Examples

Package Files

table.go

func NewWriter Uses

func NewWriter(w io.Writer) *csv.Writer

NewWriter creates a writer which appends records to a CSV raw file.

As returned by NewWriter, a csv.Writer writes records terminated by a newline and uses ',' as the field delimiter. The exported fields can be changed to customize the details before the first call to Write or WriteAll.

Comma is the field delimiter.

If UseCRLF is true, the csv.Writer ends each record with \r\n instead of \n.

Code:

var buf bytes.Buffer
w := NewWriter(&buf)
w.Write([]string{"foo", "bar"})
w.Flush()
fmt.Println(buf.String())

Output:

foo,bar

type CreationOpts Uses

type CreationOpts func(t *Table) error

CreationOpts defines functional options for creating Tables.

func ConsiderInitialSpace Uses

func ConsiderInitialSpace() CreationOpts

ConsiderInitialSpace configures the CSV parser to treat the whitespace immediately after a delimiter as part of the following field.

func Delimiter Uses

func Delimiter(d rune) CreationOpts

Delimiter specifies the character sequence which should separate fields (aka columns).

func LoadHeaders Uses

func LoadHeaders() CreationOpts

LoadHeaders uses the first line of the CSV as table headers. The header line will be skipped during iteration

func SetHeaders Uses

func SetHeaders(headers ...string) CreationOpts

SetHeaders sets the table headers.

type Source Uses

type Source func() (io.ReadCloser, error)

Source defines a table physical data source.

func FromFile Uses

func FromFile(path string) Source

FromFile defines a file-based Source.

func FromString Uses

func FromString(str string) Source

FromString defines a string-based source.

func Remote Uses

func Remote(url string) Source

Remote fetches the source schema from a remote URL.

type Table Uses

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

Table represents a Table backed by a CSV physical representation.

func NewTable Uses

func NewTable(source Source, opts ...CreationOpts) (*Table, error)

NewTable creates a table.Table from the CSV table physical representation. CreationOpts are executed in the order they are declared. If a dialect is not configured via SetDialect, DefautltDialect is used.

func (*Table) Headers Uses

func (table *Table) Headers() []string

Headers returns the headers of the tabular data.

func (*Table) Iter Uses

func (table *Table) Iter() (table.Iterator, error)

Iter returns an Iterator to read the table. Iter returns an error if the table physical source can not be iterated. The iteration process always start at the beginning of the CSV and is backed by a new reading.

Code:

table, _ := NewTable(FromString("\"name\"\nfoo\nbar"), LoadHeaders())
iter, _ := table.Iter()
defer iter.Close()
for iter.Next() {
    fmt.Println(iter.Row())
}

Output:

[foo]
[bar]

func (*Table) ReadAll Uses

func (table *Table) ReadAll() ([][]string, error)

ReadAll reads all rows from the table and return it as strings.

Code:

table, _ := NewTable(FromString("\"name\"\nfoo\nbar"), LoadHeaders())
rows, _ := table.ReadAll()
fmt.Print(rows)

Output:

[[foo] [bar]]

func (*Table) String Uses

func (table *Table) String() string

String returns a string version of the table.

Package csv imports 11 packages (graph) and is imported by 1 packages. Updated 2018-01-26. Refresh now. Tools for package owners.