csv

package module
v0.0.0-...-219c90c Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2019 License: MIT Imports: 3 Imported by: 0

README

Go report

csv - A package reads and slices data from a CSV file

Features

  • reads a csv with or without skipping header
  • retrieves a single row or rows
  • retrieves a single column or cols
  • retrieves a slice of data by specifiying both the row range and column indices

Example

Read a CSV file, header skipped
err := reader.Read("sample/weights.csv", true)
if err != nil {
  fmt.Println(err)
}
Retrieves the first two rows
err := reader.Read("sample/weights.csv", true)
if err != nil {
  fmt.Println(err)
}
data := reader.Rows(1, 3)
Retrieves the data of the first and the third column
err := reader.Read("sample/weights.csv", true)
if err != nil {
  fmt.Println(err)
}
data := reader.Cols(1, 3)

We also support retrieving columns by their names, E.g.,

err := reader.Read("sample/weights.csv", ',', true, []string{"date","weight-in-kg",
		"time-of-last-meal"})
if err != nil {
  fmt.Println(err)
}
data := reader.Cols("date", "time-of-last-meal")
Retrieve the rows (2, 3, 4) of the data of the first and the third column
err := reader.Read("sample/weights.csv", true)
if err != nil {
  fmt.Println(err)
}
data := reader.Slice(2, 4, 1, 3)

In this case, we also support retrieving columns by their names. E.g.,

err := reader.Read("sample/weights.csv", ',', true, []string{"date","weight-in-kg",
       		"time-of-last-meal"})
if err != nil {
  fmt.Println(err)
}
data := reader.Slice(2, 4, "date", "time-of-last-meal")

Benchmark

Task Rounds Speed Mem Alloc Allocs
Get column data (random cols) 980545 3710 ns/op 2672 B/op 54 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

Reader defines a reading structure for reading a CSV file.

func (*Reader) Col

func (reader *Reader) Col(colName interface{}) (data []string, err error)

Col retrieves a single column data given either column index or column name.

func (*Reader) Cols

func (reader *Reader) Cols(colNames ...interface{}) (data [][]string, err error)

Cols retrieves a multi-column data given either column indices or column names. It returns a 2d array even user only retrieves one column.

func (*Reader) Data

func (reader *Reader) Data() [][]string

func (*Reader) Read

func (reader *Reader) Read(fileName string, sep rune, skip bool, columns []string) error

Read reads a CSV file by specifying: - name: file name (full path) - sep: separator, e.g., ',', ';' - skip: if true first row will be skipped, otherwise not skipped - columns: column names, use can set column names and later use column name to retrieve column data.

func (*Reader) Row

func (reader *Reader) Row(index int) []string

Row retrieves a single row using row number (starts from 1).

func (*Reader) Rows

func (reader *Reader) Rows(from, to int) [][]string

Rows retrieves rows given a range(from, to). It returns a 2d array even user only retrieves one row.

func (*Reader) Slice

func (reader *Reader) Slice(from, to int, colNames ...interface{}) (data [][]string, err error)

Slice retrieves a multi-row, multi-column data given a row range and columns (either indices or names). It returns a 2d array even user only retrieves one cell.

type Writer

type Writer struct{}

Writer is for writing a CSV file.

func (*Writer) Write

func (writer *Writer) Write(data [][]string, saveAs string) error

Write writes a [][]string to a csv file

Jump to

Keyboard shortcuts

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