hep: go-hep.org/x/hep/hbook/ntup/ntcsv Index | Examples | Files

package ntcsv

import "go-hep.org/x/hep/hbook/ntup/ntcsv"

Package ntcsv provides a convenient access to CSV files as n-tuple data.

Examples:

nt, err := ntcsv.Open("testdata/simple.csv")
if err != nil {
    log.Fatal(err)
}
defer nt.DB().Close()

or, with a different configuration for the comma/comment runes:

nt, err := ntcsv.Open("testdata/simple.csv", ntcsv.Comma(' '), ntcsv.Comment('#'))
if err != nil {
    log.Fatal(err)
}
defer nt.DB().Close()

Give our own names to the CSV columns (default: "var1", "var2", ...):

nt, err := ntcsv.Open("testdata/simple.csv", ntcsv.Columns("var1", "i64", "foo"))

Take the names from the CSV header (note that the header *must* exist):

nt, err := ntcsv.Open("testdata/simple-with-header.csv", ntcsv.Header())

Override the names from the CSV header with our own:

nt, err := ntcsv.Open("testdata/simple-with-header.csv", ntcsv.Header(), ntcsv.Columns("v1", "v2", "v3")

Index

Examples

Package Files

ntcsv.go

func Open Uses

func Open(name string, opts ...Option) (*ntup.Ntuple, error)

Open opens a CSV file in read-only mode and returns a n-tuple connected to that.

Code:

// Open a new n-tuple pointing at a CSV file "testdata/simple.csv"
// whose field separator is ';'.
// We rename the columns v1, v2 and v3.
nt, err := ntcsv.Open(
    "testdata/simple.csv",
    ntcsv.Comma(';'),
    ntcsv.Columns("v1", "v2", "v3"),
)
if err != nil {
    log.Fatal(err)
}

defer nt.DB().Close()

err = nt.Scan("v1, v2, v3", func(i int64, f float64, s string) error {
    fmt.Printf("%d %f %q\n", i, f, s)
    return nil
})
if err != nil {
    log.Fatal(err)
}

Output:

0 0.000000 "str-0"
1 1.000000 "str-1"
2 2.000000 "str-2"
3 3.000000 "str-3"
4 4.000000 "str-4"
5 5.000000 "str-5"
6 6.000000 "str-6"
7 7.000000 "str-7"
8 8.000000 "str-8"
9 9.000000 "str-9"

Code:

// Open a new n-tuple pointing at a remote CSV file
// "https://github.com/go-hep/hep/raw/master/hbook/ntup/ntcsv/testdata/simple.csv"
// whose field separator is ';'.
// We rename the columns v1, v2 and v3.
nt, err := ntcsv.Open(
    "https://github.com/go-hep/hep/raw/master/hbook/ntup/ntcsv/testdata/simple.csv",
    ntcsv.Comma(';'),
    ntcsv.Columns("v1", "v2", "v3"),
)
if err != nil {
    log.Fatal(err)
}

defer nt.DB().Close()

err = nt.Scan("v1, v2, v3", func(i int64, f float64, s string) error {
    fmt.Printf("%d %f %q\n", i, f, s)
    return nil
})
if err != nil {
    log.Fatal(err)
}

Output:

0 0.000000 "str-0"
1 1.000000 "str-1"
2 2.000000 "str-2"
3 3.000000 "str-3"
4 4.000000 "str-4"
5 5.000000 "str-5"
6 6.000000 "str-6"
7 7.000000 "str-7"
8 8.000000 "str-8"
9 9.000000 "str-9"

Code:

// Open a new n-tuple pointing at a CSV file "testdata/simple.csv"
// whose field separator is ';'.
// We use the default column names: var1, var2, var3, ...
nt, err := ntcsv.Open(
    "testdata/simple.csv",
    ntcsv.Comma(';'),
)
if err != nil {
    log.Fatal(err)
}

defer nt.DB().Close()

err = nt.Scan("var1, var2, var3", func(i int64, f float64, s string) error {
    fmt.Printf("%d %f %q\n", i, f, s)
    return nil
})
if err != nil {
    log.Fatal(err)
}

Output:

0 0.000000 "str-0"
1 1.000000 "str-1"
2 2.000000 "str-2"
3 3.000000 "str-3"
4 4.000000 "str-4"
5 5.000000 "str-5"
6 6.000000 "str-6"
7 7.000000 "str-7"
8 8.000000 "str-8"
9 9.000000 "str-9"

Code:

// Open a new n-tuple pointing at a CSV file "testdata/simple.csv"
// whose field separator is ';'.
// We rename the columns v1, v2 and v3.
// We tell the CSV driver to handle the CSV header.
nt, err := ntcsv.Open(
    "testdata/simple-with-header.csv",
    ntcsv.Comma(';'),
    ntcsv.Header(),
    ntcsv.Columns("v1", "v2", "v3"),
)
if err != nil {
    log.Fatal(err)
}

defer nt.DB().Close()

err = nt.Scan("v1, v2, v3", func(i int64, f float64, s string) error {
    fmt.Printf("%d %f %q\n", i, f, s)
    return nil
})
if err != nil {
    log.Fatal(err)
}

Output:

0 0.000000 "str-0"
1 1.000000 "str-1"
2 2.000000 "str-2"
3 3.000000 "str-3"
4 4.000000 "str-4"
5 5.000000 "str-5"
6 6.000000 "str-6"
7 7.000000 "str-7"
8 8.000000 "str-8"
9 9.000000 "str-9"

Code:

// Open a new n-tuple pointing at a CSV file "testdata/simple.csv"
// whose field separator is ';'.
// We tell the CSV driver to handle the CSV header.
// And we explicitly use our column names for the queries.
nt, err := ntcsv.Open(
    "testdata/simple-with-header.csv",
    ntcsv.Comma(';'),
    ntcsv.Header(),
    ntcsv.Columns("v1", "v2", "v3"),
)
if err != nil {
    log.Fatal(err)
}

defer nt.DB().Close()

err = nt.Scan("v1, v2, v3", func(i int64, f float64, s string) error {
    fmt.Printf("%d %f %q\n", i, f, s)
    return nil
})
if err != nil {
    log.Fatal(err)
}

Output:

0 0.000000 "str-0"
1 1.000000 "str-1"
2 2.000000 "str-2"
3 3.000000 "str-3"
4 4.000000 "str-4"
5 5.000000 "str-5"
6 6.000000 "str-6"
7 7.000000 "str-7"
8 8.000000 "str-8"
9 9.000000 "str-9"

Code:

// Open a new n-tuple pointing at a CSV file "testdata/simple.csv"
// whose field separator is ';'.
// We tell the CSV driver to handle the CSV header.
// And we implicitly use the column names for the queries.
nt, err := ntcsv.Open(
    "testdata/simple-with-header.csv",
    ntcsv.Comma(';'),
    ntcsv.Header(),
)
if err != nil {
    log.Fatal(err)
}

defer nt.DB().Close()

err = nt.Scan("i, f, str", func(i int64, f float64, s string) error {
    fmt.Printf("%d %f %q\n", i, f, s)
    return nil
})
if err != nil {
    log.Fatal(err)
}

Output:

0 0.000000 "str-0"
1 1.000000 "str-1"
2 2.000000 "str-2"
3 3.000000 "str-3"
4 4.000000 "str-4"
5 5.000000 "str-5"
6 6.000000 "str-6"
7 7.000000 "str-7"
8 8.000000 "str-8"
9 9.000000 "str-9"

type Option Uses

type Option func(c *csvdriver.Conn)

Option configures the underlying sql.DB connection to the n-tuple.

func Columns Uses

func Columns(names ...string) Option

Columns names the n-tuple columns with the given slice.

func Comma Uses

func Comma(v rune) Option

Comma configures the n-tuple to use v as the comma delimiter between columns.

func Comment Uses

func Comment(v rune) Option

Comment configures the n-tuple to use v as the comment character for start of line.

func Header() Option

Header informs the n-tuple the CSV file has a header line.

Package ntcsv imports 2 packages (graph). Updated 2018-08-02. Refresh now. Tools for package owners.