csvier

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2019 License: MIT Imports: 3 Imported by: 0

README

csvier

This Go module simplifies reading of CSV files. Parsed data is represented as a slice of maps.

Example

The most basic example:

package main

import (
    "fmt"

    "github.com/fabritsius/csvier"
)

func main() {
    data, err := csvier.Read("data.csv")
    if err != nil {
        panic(err)
    }

    for _, r := range data {
        fmt.Printf("%s (age %s) believes that %s.\n", r["NAME"], r["AGE"], r["BELIEF"])
    }
}

Output:

Tommy (age 6) believes that dragons are real.
Steve (age 29) believes that you can always win with a clear conscience.
Bill (age 49) believes that the Earth is flat.

You can find this example with data in this gist.

Features

  • use Read(filename) to parse a CSV file
  • by default first line is used to name all the fields
  • function Read can take additional functional options:
    • use Index([slice, of, names]) to set custom names to each column
    • use Skip(nrows) to skip N rows from the beginning
    • use Limit(nrows) to limit amount of returned rows
    • use Delimiter('rune') to change default comma separator

Please see examples of use in csvier_test.go file.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delimiter

func Delimiter(d rune) func(*config) error

Delimiter is an option for Read() and allows to change csv delimiter

Example
package main

import (
	"fmt"

	"github.com/fabritsius/csvier"
)

func main() {
	data, err := csvier.Read("./test_data/data.tsv",
		csvier.Limit(5),
		csvier.Delimiter('\t'),
	)
	if err != nil {
		panic(err)
	}

	for _, r := range data {
		fmt.Printf("%s fights with %s\n", r["NAME"], r["WEAPON"])
	}
}
Output:

Peter fights with Quad Blasters
Gamora fights with Godslayer
Rocket fights with Ion Cannon
Groot fights with courage
Drax fights with Dual Knives

func Index

func Index(items []string) func(*config) error

Index is an option for Read() and allows to change csv index (column names)

Example
package main

import (
	"fmt"

	"github.com/fabritsius/csvier"
)

func main() {
	data, err := csvier.Read("./test_data/data.csv",
		csvier.Index([]string{"id", "name", "race", "weapon"}),
		csvier.Skip(1),
		csvier.Limit(5),
	)
	if err != nil {
		panic(err)
	}

	for _, r := range data {
		fmt.Printf("%s fights with %s.\n", r["name"], r["weapon"])
	}
}
Output:

Peter fights with Quad Blasters.
Gamora fights with Godslayer.
Rocket fights with Ion Cannon.
Groot fights with courage.
Drax fights with Dual Knives.

func Limit

func Limit(n int) func(*config) error

Limit is an option for Read() and allows to limit number of rows in the result

func Read

func Read(fileName string, options ...func(*config) error) ([]map[string]string, error)

Read function reads csv file and returns a map for each line

Example
package main

import (
	"fmt"

	"github.com/fabritsius/csvier"
)

func main() {
	data, err := csvier.Read("./test_data/data.csv")
	if err != nil {
		panic(err)
	}

	for _, r := range data {
		fmt.Printf("%s fights with %s.\n", r["NAME"], r["WEAPON"])
	}
}
Output:

Peter fights with Quad Blasters.
Gamora fights with Godslayer.
Rocket fights with Ion Cannon.
Groot fights with courage.
Drax fights with Dual Knives.
Nebula fights with Electroshock Batons.

func Skip

func Skip(n int) func(*config) error

Skip is an option for Read() and allows to skip number of rows

Types

This section is empty.

Jump to

Keyboard shortcuts

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