csvmap

package module
v0.0.0-...-cb70653 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2021 License: MIT Imports: 3 Imported by: 0

README

go-csv-map

A wrapper around golang's encoding/csv providing map-based access

Installation

go get github.com/evilchown755/go-csv-map

Usage

Basic Usage
CSV with first line as header
import (
    "bytes"
    "fmt"
    "os"

    "github.com/evilchown755/go-csv-map"
)

buf := bytes.NewBufferString("Album,Year\nDark Side of the Moon,1973\nExile On Main St,1972")
reader := csvmap.NewReader(buf)
reader.Columns, err := reader.ReadHeader()
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}
records, err := reader.ReadAll()
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}
fmt.Println(records[1]["Year"])
// Output: 1972
CSV with no header line
import (
    "bytes"
    "fmt"
    "os"

    "github.com/evilchown755/go-csv-map"
)

buf := bytes.NewBufferString("Dark Side of the Moon,1973\nExile On Main St,1972")
reader := csvmap.NewReader(buf)
reader.Columns = []string{"Album", "Year"}
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}
records, err := reader.ReadAll()
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}
fmt.Println(records[1]["Year"])
// Output: 1972
Advanced Usage

csvmap.Reader.Reader gives you access to the underlying csv.Reader object.

Changing the separator
import (
    "bytes"
    "fmt"
    "os"

    "github.com/evilchown755/go-csv-map"
)

buf := bytes.NewBufferString("Album;Year\nDark Side of the Moon;1973\nExile On Main St;1972")
reader := csvmap.NewReader(buf)
reader.Reader.Comma = ';'
reader.Columns, err := reader.ReadHeader()
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}
records, err := reader.ReadAll()
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}
fmt.Println(records[1]["Year"])
// Output: 1972
Checking for the number of fields
import (
    "bytes"
    "fmt"
    "os"

    "github.com/evilchown755/go-csv-map"
)

buf := bytes.NewBufferString("Album;Year\nDark Side of the Moon;1973\nExile On Main St;1972")
reader := csvmap.NewReader(buf)
reader.Reader.FieldsPerRecord = 3 
reader.Columns, err := reader.ReadHeader()
fmt.Println(err != nil)
// Output: true

Directory Structure

code/go-csv-map/
|-- csvmap.go
|   Main code
|-- csvmap_test.go
|   Tests
|-- examples_test.go
|   Examples
|-- .gitignore
|   Files git will ignore
|-- LICENSE
|   MIT License
|-- README.md
|   This file
`-- .travis.yml
    Travis configuration

The above file tree was generated with tree -a -L 1 --charset ascii.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	Reader  *csv.Reader
	Columns []string
}

A Reader reads records from a CSV-encoded file.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new Reader that reads from r.

Example
reader := NewReader(bytes.NewBuffer(nil))
fmt.Println(string(reader.Reader.Comma))
Output:

,

func (*Reader) Read

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

Read wraps csv.Reader.Read, creating a map of column name to field value. If the line has fewer columns than Reader.Columns, the map will not contain keys for these columns; thus we can distinguish between missing columns and columns with empty values. If the line has more columns than Reader.Columns, Reader.Read() ignores them.

Example
reader := NewReader(bytes.NewBufferString("first,last\nAlexander,Hamilton"))
var err error
reader.Columns, err = reader.ReadHeader()
if err != nil {
	fmt.Println(err)
	return
}
record, err := reader.Read()
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(record["first"])
Output:

Alexander

func (*Reader) ReadAll

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

ReadAll reads all the remaining records from r. Each record is a map of column name to field value.

Example
reader := NewReader(bytes.NewBufferString("first,last\nAlexander,Hamilton\nAaron,Burr"))
var err error
reader.Columns, err = reader.ReadHeader()
if err != nil {
	fmt.Println(err)
	return
}
records, err := reader.ReadAll()
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(records[1]["last"])
Output:

Burr

func (*Reader) ReadHeader

func (r *Reader) ReadHeader() (columns []string, err error)

ReadHeader simply wraps csv.Reader.Read().

Example
reader := NewReader(bytes.NewBufferString("first,last\nAlexander,Hamilton"))
var err error
reader.Columns, err = reader.ReadHeader()
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(reader.Columns[1])
Output:

last

Jump to

Keyboard shortcuts

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