csvdata

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2022 License: Apache-2.0 Imports: 9 Imported by: 5

README

csvdata -- Reading CSV Data

check vulns lint status GitHub license GitHub release

This package is required Go 1.16 or later.

Migrated repository to github.com/goark/csvdata

Import

import "github.com/goark/csvdata"

Usage

package csvdata_test

import (
	"fmt"

	"github.com/goark/csvdata"
)

func ExampleNew() {
	file, err := csvdata.OpenFile("testdata/sample.csv")
	if err != nil {
		fmt.Println(err)
		return
	}
	rc := csvdata.NewRows(csvdata.New(file), true)
	defer rc.Close()

	if err := rc.Next(); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(rc.Column("name"))
	// Output:
	// Mercury
}
Reading from Excel file
package exceldata_test

import (
	"fmt"

	"github.com/goark/csvdata"
	"github.com/goark/csvdata/exceldata"
)

func ExampleNew() {
	xlsx, err := exceldata.OpenFile("testdata/sample.xlsx", "")
	if err != nil {
		fmt.Println(err)
		return
	}
	r, err := exceldata.New(xlsx, "")
	if err != nil {
		fmt.Println(err)
		return
	}
	rc := csvdata.NewRows(r, true)
	defer rc.Close() //dummy

	if err := rc.Next(); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(rc.Column("name"))
	// Output:
	// Mercury
}
Reading from LibreOffice Calc file
package calcdata_test

import (
	"fmt"

	"github.com/goark/csvdata"
	"github.com/goark/csvdata/calcdata"
)

func ExampleNew() {
	ods, err := calcdata.OpenFile("testdata/sample.ods")
	if err != nil {
		fmt.Println(err)
		return
	}
	r, err := calcdata.New(ods, "")
	if err != nil {
		fmt.Println(err)
		return
	}
	rc := csvdata.NewRows(r, true)
	defer rc.Close() //dummy

	if err := rc.Next(); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(rc.Column("name"))
	// Output:
	// Mercury
}

Modules Requirement Graph

dependency.png

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNullPointer      = errors.New("null reference instance")
	ErrNoData           = errors.New("no data")
	ErrInvalidRecord    = errors.New("invalid record")
	ErrOutOfIndex       = errors.New("out of index")
	ErrInvalidSheetName = errors.New("invalid sheet name in Excel data")
	ErrInvalidExcelData = errors.New("invalid Excel data")
)

Functions

func OpenFile

func OpenFile(path string) (*os.File, error)

OpenFile returns CSV file Reader.

Types

type Reader

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

Reader is class of CSV reader

func New

func New(r io.Reader) *Reader

New function creates a new Reader instance.

Example
package main

import (
	"fmt"

	"github.com/goark/csvdata"
)

func main() {
	file, err := csvdata.OpenFile("testdata/sample.csv")
	if err != nil {
		fmt.Println(err)
		return
	}
	rc := csvdata.NewRows(csvdata.New(file), true)
	defer rc.Close()

	if err := rc.Next(); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(rc.Column("name"))
}
Output:

Mercury

func (*Reader) Close

func (r *Reader) Close() error

func (*Reader) Read

func (r *Reader) Read() ([]string, error)

Read method returns next row data.

func (*Reader) WithComma

func (r *Reader) WithComma(c rune) *Reader

WithComma method sets Comma property.

func (*Reader) WithFieldsPerRecord

func (r *Reader) WithFieldsPerRecord(size int) *Reader

WithFieldsPerRecord method sets FieldsPerRecord property.

type Rows

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

Rows is a accesser for row-column data set.

func NewRows

func NewRows(rr RowsReader, headerFlag bool) *Rows

func (*Rows) Close

func (r *Rows) Close() error

Close method is closing RowsReader instance.

func (*Rows) Column

func (r *Rows) Column(s string) string

GetString method returns string data in current row.

func (*Rows) ColumnBool

func (r *Rows) ColumnBool(s string) (bool, error)

ColumnBool method returns type bool data in current row.

func (*Rows) ColumnFloat64

func (r *Rows) ColumnFloat64(s string) (float64, error)

ColumnFloat method returns type float64 data in current row.

func (*Rows) ColumnInt64

func (r *Rows) ColumnInt64(s string, base int) (int64, error)

ColumnInt method returns type int64 data in current row.

func (*Rows) ColumnNullBool

func (r *Rows) ColumnNullBool(s string) (sql.NullBool, error)

ColumnNullBool method returns sql.NullBool data in current row.

func (*Rows) ColumnNullByte

func (r *Rows) ColumnNullByte(s string, base int) (sql.NullByte, error)

ColumnNullByte method returns sql.NullByte data in current row.

func (*Rows) ColumnNullFloat64

func (r *Rows) ColumnNullFloat64(s string) (sql.NullFloat64, error)

ColumnNullFloat64 method returns sql.NullFloat64 data in current row.

func (*Rows) ColumnNullInt16

func (r *Rows) ColumnNullInt16(s string, base int) (sql.NullInt16, error)

ColumnNullInt16 method returns sql.NullFloat64 data in current row.

func (*Rows) ColumnNullInt32

func (r *Rows) ColumnNullInt32(s string, base int) (sql.NullInt32, error)

ColumnNullInt32 method returns sql.NullInt32 data in current row.

func (*Rows) ColumnNullInt64

func (r *Rows) ColumnNullInt64(s string, base int) (sql.NullInt64, error)

ColumnNullInt64 method returns sql.NullInt64 data in current row.

func (*Rows) ColumnNullString

func (r *Rows) ColumnNullString(s string) (sql.NullString, error)

ColumnNullString method returns ql.NullString data in current row.

func (*Rows) ColumnString

func (r *Rows) ColumnString(s string) (string, error)

ColumnString method returns string data in current row.

func (Rows) Get

func (r Rows) Get(i int) string

GetString method returns string data in current row.

func (*Rows) GetBool

func (r *Rows) GetBool(i int) (bool, error)

GetBool method returns type bool data in current row.

func (*Rows) GetFloat64

func (r *Rows) GetFloat64(i int) (float64, error)

GetFloat method returns type float64 data in current row.

func (*Rows) GetInt64

func (r *Rows) GetInt64(i int, base int) (int64, error)

GetInt method returns type int64 data in current row.

func (*Rows) GetString

func (r *Rows) GetString(i int) (string, error)

GetString method returns string data in current row.

func (*Rows) Header

func (r *Rows) Header() ([]string, error)

Header method returns header strings.

func (*Rows) Next

func (r *Rows) Next() error

Next method gets a next record.

func (*Rows) Row

func (r *Rows) Row() []string

Row method returns current row data.

type RowsReader

type RowsReader interface {
	Read() ([]string, error)
	Close() error
}

RowsReader is interface type for reading columns in a row.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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