csv

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2022 License: MIT Imports: 13 Imported by: 6

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewWriter added in v0.1.2

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.

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

foo,bar

Types

type CreationOpts

type CreationOpts func(t *Table) error

CreationOpts defines functional options for creating Tables.

func ConsiderInitialSpace added in v1.1.2

func ConsiderInitialSpace() CreationOpts

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

func Delimiter added in v1.1.2

func Delimiter(d rune) CreationOpts

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

func LoadHeaders

func LoadHeaders() CreationOpts

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

func SetHeaders

func SetHeaders(headers ...string) CreationOpts

SetHeaders sets the table headers.

type Source

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

Source defines a table physical data source.

func FromFile

func FromFile(path string) Source

FromFile defines a file-based Source from a CSV or GZIP compressed CSV path.

func FromString

func FromString(str string) Source

FromString defines a string-based source.

func Remote added in v0.1.3

func Remote(url string) Source

Remote fetches the source schema from a remote URL.

type Table

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

Table represents a Table backed by a CSV physical representation.

func NewTable added in v0.1.2

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

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

Headers returns the headers of the tabular data.

func (*Table) Iter

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.

Example
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 added in v0.1.2

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

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

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

[[foo] [bar]]

func (*Table) ReadColumn added in v1.1.2

func (table *Table) ReadColumn(name string) ([]string, error)

ReadColumn reads a specific column from the table and return it as strings.

Example
table, _ := NewTable(FromString("name,age\nfoo,25\nbar,48"), LoadHeaders())
cols, _ := table.ReadColumn("name")
fmt.Print(cols)
Output:

[foo bar]

func (*Table) String added in v1.1.2

func (table *Table) String() string

String returns a string version of the table.

Jump to

Keyboard shortcuts

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