customcsv

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2021 License: MIT Imports: 5 Imported by: 3

README

go-customcsv

Go Reference Test codecov

This is a library for customizing the CSV format.

You can customize the following.

  • (Reader/Writer) Format characters
    • Delimiter (default: ,)
    • Quote (default: ")
    • Record separator (default: \r\n)
  • (Writer) Always quote (default: false)
  • (Reader) Verify the number of fields per record (default: Check by the number of fields in the first record)

In Reader, the head BOM will be automatically skipped.

Usage

Reader
f, err := os.Open("input.csv")
if err != nil {
	return err
}

r := customcsv.NewReader(f)

for {
	record, err := r.Read()
	if err == io.EOF {
		break
	}
	if err != nil {
    	return err
	}

    fmt.Println(record)
}

In Reader, the following items can be customized.

type Reader struct {
	// Delimiter is the field delimiter.
	// It is set to default comma (',') by NewReader.
	Delimiter rune

	// Quote is the field quote character.
	// It is set to default double quote ('"') by NewReader.
	Quote rune

	// SpecialRecordSeparator is the special record separator.
	// If not specified, a newline ('\n' '\r' '\r\n') will be used as the record separator.
	SpecialRecordSeparator string

	// FieldsPerRecord is the number of expected fields per record.
	// FieldsPerRecord > 0 : Checks for the specified value.
	// FieldsPerRecord = 0 : Check by the number of fields in the first record.
	// FieldsPerRecord < 0 : No check.
	FieldsPerRecord int
}

Set this after NewReader().

r := customcsv.NewReader(f)

// Customize format
r.Delimiter = ';'
r.SpecialRecordSeparator = "|"
Writer
f, err := os.Create("output.csv")
if err != nil {
	return err
}

w := customcsv.NewWriter(f)

for _, record := range records {
	if err := w.Write(record); err != nil {
		return err
	}
}

if err := w.Flush(); err != nil {
	return err
}

In Writer, the following items can be customized.

type Writer struct {
	// Delimiter is the field delimiter.
	// It is set to default comma (',') by NewWriter.
	Delimiter rune

	// Quote is the field quote character.
	// It is set to default double quote ('"') by NewWriter.
	Quote rune

	// If True, always quote the fields.
	AllQuotes bool

	// RecordSeparator is the record separator.
	// It is set to default CRLF ('\r\n') by NewWriter.
	RecordSeparator string
}

Set this after NewWriter().

w := customcsv.NewWriter(f)

// Customize format
w.AllQuotes = true
w.RecordSeparator = "\n"

License

MIT

Author

onozaty

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ParseError

type ParseError struct {
	Message string
	Record  int
	Column  int
}

func (*ParseError) Error

func (e *ParseError) Error() string

type Reader

type Reader struct {
	// Delimiter is the field delimiter.
	// It is set to default comma (',') by NewReader.
	Delimiter rune

	// Quote is the field quote character.
	// It is set to default double quote ('"') by NewReader.
	Quote rune

	// SpecialRecordSeparator is the special record separator.
	// If not specified, a newline ('\n' '\r' '\r\n') will be used as the record separator.
	SpecialRecordSeparator string

	// FieldsPerRecord is the number of expected fields per record.
	// FieldsPerRecord > 0 : Checks for the specified value.
	// FieldsPerRecord = 0 : Check by the number of fields in the first record.
	// FieldsPerRecord < 0 : No check.
	FieldsPerRecord int
	// contains filtered or unexported fields
}

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) Read

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

func (*Reader) ReadAll

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

type Writer

type Writer struct {
	// Delimiter is the field delimiter.
	// It is set to default comma (',') by NewWriter.
	Delimiter rune

	// Quote is the field quote character.
	// It is set to default double quote ('"') by NewWriter.
	Quote rune

	// If True, always quote the fields.
	AllQuotes bool

	// RecordSeparator is the record separator.
	// It is set to default CRLF ('\r\n') by NewWriter.
	RecordSeparator string
	// contains filtered or unexported fields
}

func NewWriter

func NewWriter(w io.Writer) *Writer

func (*Writer) Flush

func (w *Writer) Flush() error

func (*Writer) Write

func (w *Writer) Write(record []string) error

func (*Writer) WriteAll

func (w *Writer) WriteAll(records [][]string) error

Jump to

Keyboard shortcuts

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