csvutil

package module
v0.0.0-...-9dfce04 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2016 License: BSD-2-Clause Imports: 9 Imported by: 0

README

csvutil - Your CSV pocket-knife (golang)

#WARNING

I would advise against using this package. It was a language learning exercise from a time before "encoding/csv" existed. If encoding/csv doesn't fit your needs I would advise finding a different maintained package or writing a new one

Version

This is csvutil version 1.1_2

Synopsis

The csvutil package can be used to read CSV data from any io.Reader and, write CSV data to any io.Writer. It can automatically generate CSV rows from slices containing native Go types, other slices/arrays of native go types, or flat structs. It can also convert CSV rows and assign values to memory referenced by a slice of pointers.

package main
import "os"
import "github.com/bmatsuo/csvutil"

type Person struct {
    Name   string
    Height float64
    Weight float64
}

func main() {
    writer := csvutil.NewWriter(os.Stdout, nil)
    errdo := csvutil.DoFile(os.Args[1], func(r csvutil.Row) bool {
        if r.HasError() {
            panic(r.Error)
        }
        var person Person
        if _, errc := r.Format(&person); errc != nil {
            panic("Row is not a Person")
        }
        bmi := person.Weight / (person.Height * person.Height)
        writer.WriteRow(csvutil.FormatRow(person, bmi).Fields...)
        return true
    })
    if errdo != nil {
        panic(errdo)
    }
    writer.Flush()
}

Given a CSV file 'in.csv' with contents

alice,1.4,50
bob,2,80
chris,1.6,67

When the above program is called and given the path 'in.csv', the following is printed to standard output.

alice,1.4,50,25.510204081632658
bob,2,80,20
chris,1.6,67,26.171874999999996

About

The csvutil package is written to make interacting with CSV data as easy as possible. Efficiency of its underlying functions and methods are slightly less important factors in its design. However, that being said, it is important. And, csvutil should be capable of handling both extremely large and fairly small streams of CSV data through input and output quite well in terms of speed and memory footprint. It should do this while not making your code bend over backwards (more than necessary). As libraries should never make you do.

Features

  • Slurping/spewing CSV data. That is, reading/writing whole files or data streams at once.

  • Iteration through individual rows of a CSV data stream for a smaller memory footprint.

  • Writing of individual writing fields/rows (along with batch writing).

  • Automated CSV row serialization and deserialization (formatting) for flat data structures and types.

Todo

  • Enhance and clean the formatting API and allow better formatting of data.

Install

The easiest installation of csvutil is done through goinstall.

goinstall github.com/bmatsuo/csvutil

Documentation

The best way to read the current csvutil documentation is using godoc.

godoc github.com/bmatsuo/csvutil

Or better yet, you can run a godoc http server.

godoc -http=":6060"

Then go to the url http://localhost:6060/pkg/github.com/bmatsuo/csvutil/

Copyright (c) 2011, Bryan Matsuo. All rights reserved.

Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Documentation

Overview

Package csvutil is not maintained. It provides formatted reading and writing of CSV data.

Index

Constants

This section is empty.

Variables

View Source
var (
	FloatFmt  byte = 'g'
	FloatPrec int  = -1
)

See strconv.Ftoa32()

View Source
var (
	ErrorIndex         = errors.New("Not enough fields to format")
	ErrorStruct        = errors.New("Cannot format unreferenced structs")
	ErrorUnimplemented = errors.New("Unimplemented field type")
	ErrorFieldType     = errors.New("Field type incompatible.")
	ErrorNonPointer    = errors.New("Target is not a pointer.")
	ErrorCantSet       = errors.New("Cannot set value.")
)
View Source
var (
	DefaultConfig = &Config{
		Sep: ',', Trim: false, Cutset: " \t", CommentPrefix: "#",
		Comments: false, CommentsInBody: false}
)

The default configuration is used for Readers and Writers when none is given.

Functions

func Do

func Do(r io.Reader, f func(r Row) bool)

Iteratively apply a function to Row objects read from an io.Reader.

func DoFile

func DoFile(filename string, f func(r Row) bool) error

Iteratively apply a function to Row objects read from a named file.

func Read

func Read(r io.Reader) ([][]string, error)

Read rows from an io.Reader until EOF is encountered.

func ReadFile

func ReadFile(filename string) ([][]string, error)

Read a named CSV file into a new slice of new string slices.

func Write

func Write(w io.Writer, rows [][]string) (int, error)

Write a slice of rows (string slices) to an io.Writer object.

func WriteFile

func WriteFile(filename string, perm os.FileMode, rows [][]string) (int, error)

Write CSV data to a named file. If the file does not exist, it is created. If the file exists, it is truncated upon opening. Requires that file permissions be specified. Recommended permissions are 0600, 0622, and 0666 (6:rw, 4:w, 2:r).

Types

type Config

type Config struct {
	// General configuration
	//  Field seperator
	Sep rune
	//  Trim leading/trailing whitespace in fields.
	Trim bool
	//  Characters to trim from fields.
	Cutset string
	//  Prefix for comment lines.
	CommentPrefix string

	// Reader specific config
	//  Are comments allowed in the input.
	Comments bool
	//  Comments can appear in the body (Comments must be true).
	CommentsInBody bool
}

A configuration structure that can be shared between a Reader and Writer.

func NewConfig

func NewConfig() *Config

Return a freshly allocated Config that is initialized to DefaultConfig.

func (*Config) IsSep

func (c *Config) IsSep(rune rune) bool

func (*Config) LooksLikeComment

func (c *Config) LooksLikeComment(line string) bool

type Reader

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

A reader object for CSV data utilizing the bufio package.

func NewReader

func NewReader(r io.Reader, c *Config) *Reader

Create a new reader object.

func NewReaderSize

func NewReaderSize(r io.Reader, c *Config, size int) *Reader

Create a new reader with a buffer of a specified size.

func (*Reader) Do

func (csvr *Reader) Do(f func(Row) bool)

Iteratively read the remaining rows in the reader and call f on each of them. If f returns false, no more rows will be read.

func (*Reader) DoN

func (csvr *Reader) DoN(n int, f func(Row) bool)

Process rows from the reader like Do, but stop after processing n of them. If f returns false before n rows have been process, no more rows will be processed.

func (*Reader) LineNum

func (csvr *Reader) LineNum() int

Returns the number of lines of input read by the Reader

func (*Reader) ReadRow

func (csvr *Reader) ReadRow() Row

Attempt to read up to a new line, skipping any comment lines found in the process. Return a Row object containing the fields read and any error encountered.

func (*Reader) ReadRows

func (csvr *Reader) ReadRows(rbuf [][]string) (int, error)

Read rows into a preallocated buffer. Return the number of rows read, and any error encountered.

func (*Reader) RemainingRows

func (csvr *Reader) RemainingRows() (rows [][]string, err error)

Reads any remaining rows of CSV data in the underlying io.Reader.

func (*Reader) RemainingRowsSize

func (csvr *Reader) RemainingRowsSize(size int) ([][]string, error)

Like csvr.RemainingRows(), but allows specification of the initial row buffer capacity to avoid unnecessary reallocations.

type Row

type Row struct {
	Fields []string "CSV row field data"
	Error  error    "Error encountered reading"
}

A simple row structure for rows read by a csvutil.Reader that encapsulates any read error enountered along with any data read prior to encountering an error.

func FormatRow

func FormatRow(x ...interface{}) Row

Iteratively take values from the argument list and formats them (or their elements/fields) as a (list of) string(s). Returns a Row object that contains the formatted arguments, as well as any error that occured.

func (Row) Format

func (r Row) Format(x ...interface{}) (int, error)

Iteratively take values from the argument list and assigns to them successive fields from the row object. Returns the number of row fields assigned to arguments and any error that occurred.

func (Row) HasEOF

func (r Row) HasEOF() bool

A wrapper for the test r.Error == os.EOF

func (Row) HasError

func (r Row) HasError() bool

A wrapper for the test r.Error != nil

type Writer

type Writer struct {
	*Config
	// contains filtered or unexported fields
}

A simple CSV file writer using the package bufio for effeciency. But, because of this, the method Flush() must be called to ensure data is written to any given io.Writer before it is closed.

func NewWriter

func NewWriter(w io.Writer, c *Config) *Writer

Create a new CSV writer with the default field seperator and a buffer of a default size.

func NewWriterSize

func NewWriterSize(w io.Writer, c *Config, n int) (*Writer, error)

Create a new CSV writer using a buffer of at least n bytes.

See bufio.NewWriterSize(io.Writer, int) (*bufio.NewWriter).

func (*Writer) Flush

func (csvw *Writer) Flush() error

Flush any buffered data to the underlying io.Writer.

func (*Writer) WriteComments

func (csvw *Writer) WriteComments(comments ...string) (int, error)

Write a comment. Each comment string given will start on a new line. If the string is contains multiple lines, comment prefixes will be inserted at the beginning of each one.

func (*Writer) WriteFields

func (csvw *Writer) WriteFields(fields ...string) (int, error)

Write a slice of field values with a trailing field seperator (no '\n'). Returns any error incurred from writing.

func (*Writer) WriteRow

func (csvw *Writer) WriteRow(fields ...string) (int, error)

Write a slice of field values with a trailing new line '\n'. Returns any error incurred from writing.

func (*Writer) WriteRows

func (csvw *Writer) WriteRows(rows [][]string) (int, error)

Write multple CSV rows at once.

Jump to

Keyboard shortcuts

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