ntcsv

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2019 License: BSD-3-Clause, BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

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.

Example
package main

import (
	"fmt"
	"log"

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

func main() {
	// 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"
Example (FromRemote)
package main

import (
	"fmt"
	"log"

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

func main() {
	// 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"
Example (WithDefaultVarNames)
package main

import (
	"fmt"
	"log"

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

func main() {
	// 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"
Example (WithHeader)
package main

import (
	"fmt"
	"log"

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

func main() {
	// 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"
Example (WithHeaderAndExlicitColumns)
package main

import (
	"fmt"
	"log"

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

func main() {
	// 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"
Example (WithHeaderAndImplicitColumns)
package main

import (
	"fmt"
	"log"

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

func main() {
	// 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"

Types

type Option

type Option func(c *csvdriver.Conn)

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

func Columns

func Columns(names ...string) Option

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

func Comma

func Comma(v rune) Option

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

func Comment

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.

Jump to

Keyboard shortcuts

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