Code Monkey home page Code Monkey logo

csvutil's Introduction

CSVUtil

Provides tools to help set Go structures from CSV lines / files and vice versa.

Installation

To install assertions run:

$ go get github.com/rzajac/csvutil

Documentation

Set fields from CSV

Here we are setting person fields from CSV. The columns in CSV line must be in the same order as fields in the structure (from top to bottom). You may skip fields by tagging them with csv:"-".

import (
	"github.com/rzajac/csvutil"
)

var testCsvLines = []string{"Tony|23|123.456|Y", "John|34|234.567|N|"}

type person struct {
	Name    string
	Age     int
	Skipped string `csv:"-"` // Skip this field when setting the structure
	Balance float32
	LowBalance bool
}

main () {
	// This can be any io.ReadCloser() interface
	sr := csvutil.StringReadCloser(strings.Join(testCsvLines, "\n"))

	// Set delimiter to '|', allow for trailing comma and do not check fields per CSV record
	c := csvutil.NewCsvUtil(sr).Comma('|').TrailingComma(true).FieldsPerRecord(-1).CustomBool([]string{"Y"}, []string{"N"})

	// Struct we will populate with CSV data
	p := &person{Skipped: "aaa"}

	// Set values from CSV line to person structure
	err := c.SetData(p)

	// Do work with p

	// Set values from the second CSV line
	err := c.SetData(p)

}

Picking only CSV columns we are interested in

type person2 struct {
	Name    string
	Balance float32
}

main () {
	sr := csvutil.StringReadCloser(strings.Join(testCsvLines, "\n"))

	// Set delimiter to '|', allow for trailing comma and do not check fields per CSV record
	c := csvutil.NewCsvUtil(sr).Comma('|').TrailingComma(true).FieldsPerRecord(-1)

	// Set header with column names matching structure fields and column indexes on the CSV line.
	// The indexes in the CSV line start with 0.
	c.Header(map[string]int{"Name": 0, "Balance": 2})

	// Struct we wil lpopulate with data
	p := &person2{}

	// Set values from CSV line to person2 structure
	err := c.SetData(p)

	// Do work with p

	// Set values from the second CSV line
	err := c.SetData(p)
}

Custom true / false values

CustomBool() method allows you to set custom true / false values in CSV columns.

main () {
	sr := csvutil.StringReadCloser("Y|N")
	c := csvutil.NewCsvUtil(sr).CustomBool([]string{"Y"}, []string{"N"})
}

Trim CSV column values before assigning to structure field

c := csvutil.NewCsvUtil(sr).Trim(" ") // Trim spaces from beginning and the end of volumn value
c := csvutil.NewCsvUtil(sr).Trim(" *") // Trim spaces and asterisks from beginning and the end of volumn value

Create CSV line from struct

p := &person{"Tom", 45, "aaa", 111.22, true}

csvLine, err := csvutil.ToCsv(p, "|", "YY", "NN")
fmt.Println(csvLine) // Prints: Tom|45|111.22|YY

Getting last CSV line that have been read form the file

...
p := &person2{}
err := c.SetData(p)
csvLine := c.LastCsvLine()

TODO

  • Add writing CSV to file

License

Released under the MIT License. CSVUtil (c) Rafal Zajac [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.