csv

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: Apache-2.0, BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package csv - Go package to replace stdlib encoding/csv reader - with more exposed info

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comma added in v1.1.0

type Comma rune

Comma is an option that can be used for NewReader to set Reader.Comma

type Comment added in v1.1.0

type Comment rune

Comment is an option that can be used for NewReader to set Reader.Comment

type FieldsPerRecord added in v1.1.0

type FieldsPerRecord int

FieldsPerRecord is an option that can be used for NewReader to set Reader.FieldsPerRecord

type LazyQuotes added in v1.1.0

type LazyQuotes bool

LazyQuotes is an option that can be used for NewReader to set Reader.LazyQuotes

type NoHeader added in v1.1.0

type NoHeader bool

NoHeader is an option that can be used for NewReader to set Reader.NoHeader

type NoSkipEmptyLines added in v1.3.2

type NoSkipEmptyLines bool

NoSkipEmptyLines is an option that can be used for NewReader to set Reader.NoSkipEmptyLines

type Reader

type Reader struct {
	// Comma is the field delimiter.
	// It is set to comma (',') by NewReader.
	// Comma must be a valid rune and must not be \r, \n,
	// or the Unicode replacement character (0xFFFD).
	Comma rune

	// Comment, if not 0, is the comment character. Lines beginning with the
	// Comment character without preceding whitespace are ignored.
	// With leading whitespace the Comment character becomes part of the
	// field, even if TrimLeadingSpace is true.
	// Comment must be a valid rune and must not be \r, \n,
	// or the Unicode replacement character (0xFFFD).
	// It must also not be equal to Comma.
	Comment rune

	// FieldsPerRecord is the number of expected fields per record.
	// If FieldsPerRecord is positive, Read requires each record to
	// have the given number of fields. If FieldsPerRecord is 0, Read sets it to
	// the number of fields in the first record, so that future records must
	// have the same field count. If FieldsPerRecord is negative, no check is
	// made and records may have a variable number of fields.
	FieldsPerRecord int

	// If LazyQuotes is true, a quote may appear in an unquoted field and a
	// non-doubled quote may appear in a quoted field.
	LazyQuotes bool

	// If TrimLeadingSpace is true, leading white space in a field is ignored.
	// This is done even if the field delimiter, Comma, is white space.
	TrimLeadingSpace bool

	// ReuseRecord controls whether calls to Read may return a slice sharing
	// the backing array of the previous call's returned slice for performance.
	// By default, each call to Read returns newly allocated memory owned by the caller.
	ReuseRecord bool

	// NoHeader indicates that the CSV being read does not have a header
	NoHeader bool

	// if NoSkipEmptyLines is true, empty lines are not skipped
	//
	// this solves a problem in stdlib reader - where if there's only one header field,
	// a blank line means the record has one empty field
	NoSkipEmptyLines bool
	// contains filtered or unexported fields
}

A Reader reads records from a CSV-encoded file.

As returned by NewReader, a Reader expects input conforming to RFC 4180. The exported fields can be changed to customize the details before the first call to Reader.Read or Reader.ReadAll.

The Reader converts all \r\n sequences in its input to plain \n, including in multiline field values, so that the returned data does not depend on which line-ending convention an input file uses.

func NewReader

func NewReader(r io.Reader, options ...any) *Reader

NewReader returns a new Reader that reads from r.

func (*Reader) CurrentLine added in v1.1.0

func (r *Reader) CurrentLine() int

CurrentLine returns the current line number being read

func (*Reader) FieldPos

func (r *Reader) FieldPos(field int) (line, column int)

FieldPos returns the line and column corresponding to the start of the field with the given index in the slice most recently returned by Reader.Read. Numbering of lines and columns starts at 1; columns are counted in bytes, not runes.

If this is called with an out-of-bounds index, it panics.

func (*Reader) FieldQuoted added in v1.2.0

func (r *Reader) FieldQuoted(field int) bool

FieldQuoted returns whether the specified field index was quoted

If the field index is out-of-bounds, always returns false

func (*Reader) Header

func (r *Reader) Header() ([]string, bool)

Header returns the headers (and whether header was included)

func (*Reader) InputOffset

func (r *Reader) InputOffset() int64

InputOffset returns the input stream byte offset of the current reader position. The offset gives the location of the end of the most recently read row and the beginning of the next row.

func (*Reader) RawRecord

func (r *Reader) RawRecord() []byte

RawRecord returns the raw bytes for the last record read

func (*Reader) Read

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

Read reads one record (a slice of fields) from r. If the record has an unexpected number of fields, Read returns the record along with the error [ErrFieldCount]. If the record contains a field that cannot be parsed, Read returns a partial record along with the parse error. The partial record contains all fields read before the error. If there is no data left to be read, Read returns nil, io.EOF. If [Reader.ReuseRecord] is true, the returned slice may be shared between multiple calls to Read.

func (*Reader) ReadAll

func (r *Reader) ReadAll() (records [][]string, err error)

ReadAll reads all the remaining records from r. Each record is a slice of fields. A successful call returns err == nil, not err == io.EOF. Because ReadAll is defined to read until EOF, it does not treat end of file as an error to be reported.

type ReuseRecord added in v1.3.1

type ReuseRecord bool

ReuseRecord is an option that can be used for NewReader to set Reader.ReuseRecord

type TrimLeadingSpace added in v1.1.0

type TrimLeadingSpace bool

TrimLeadingSpace is an option that can be used for NewReader to set Reader.TrimLeadingSpace

Jump to

Keyboard shortcuts

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