Your CSV pocket-knife (golang)

Overview

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 & License

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.

Issues
  • support for \n and \r\n?

    support for \n and \r\n?

    ive condition where few columns contains these values.. suggestion is to automatically detect for \n and \r\n and add in double quote wrapper option? example: ` 1,james,this is cool,2liner 2ndline,still row 1 2,blabla,...

    `

    opened by u007 0
  • Tests don't compile

    Tests don't compile

    I'm not sure whether the Go standard library has changed since this library was written, but I seem to get the following compile errors for the test suite:

    ➜  csvutil git:(master) go version
    go version go1.2 darwin/amd64
    ➜  csvutil git:(master) go test -v
    # _/Users/0x6e6562/Workspace/golang/src/github.com/bmatsuo/csvutil
    ./config_test.go:31: undefined: utf8.NewString
    ./file_test.go:17: undefined: os.ENOENT
    ./file_test.go:21: statErr.String undefined (type error has no field or method String)
    ./file_test.go:25: rmErr.String undefined (type error has no field or method String)
    ./file_test.go:40: cannot use TestPerm (type uint32) as type os.FileMode in function argument
    ./file_test.go:65: cannot use TestPerm (type uint32) as type os.FileMode in function argument
    ./file_test.go:71: err.String undefined (type error has no field or method String)
    ./reader_test.go:86: err.String undefined (type error has no field or method String)
    ./writer_test.go:21: err.String undefined (type error has no field or method String)
    FAIL    _/Users/0x6e6562/Workspace/golang/src/github.com/bmatsuo/csvutil [build failed]
    
    opened by 0x6e6562 2
Releases(0.2.7)
Owner
Bryan Matsuo
Bryan Matsuo
A fast, easy-of-use and dependency free custom mapping from .csv data into Golang structs

csvparser This package provides a fast and easy-of-use custom mapping from .csv data into Golang structs. Index Pre-requisites Installation Examples C

João Duarte 20 May 10, 2022
csvplus extends the standard Go encoding/csv package with fluent interface, lazy stream operations, indices and joins.

csvplus Package csvplus extends the standard Go encoding/csv package with fluent interface, lazy stream processing operations, indices and joins. The

Maxim 67 Apr 9, 2022
:zap: Transfer files over wifi from your computer to your mobile device by scanning a QR code without leaving the terminal.

$ qrcp Transfer files over Wi-Fi from your computer to a mobile device by scanning a QR code without leaving the terminal. You can support development

Claudio d'Angelis 8.7k Aug 17, 2022
Gotabulate - Easily pretty-print your tabular data with Go

Gotabulate - Easily pretty-print tabular data Summary Go-Tabulate - Generic Go Library for easy pretty-printing of tabular data. Installation go get g

Vadim Kravcenko 286 Jul 27, 2022
Simple HCL (HashiCorp Configuration Language) parser for your vars.

HCL to Markdown About To write a good documentation for terraform module, quite often we just need to print all our input variables as a fancy table.

Dmytro Shamenko 15 Dec 14, 2021
Preventing 3rd Party DLLs from Injecting into your Malware

Doge-BlockDLLs Preventing 3rd Party DLLs from Injecting into your Malware ACG(Arbitrary Code Guard)的方式等大佬来实现 Ref https://www.ired.team/offensive-secur

TimWhite 19 Jul 21, 2022
Stylesheet-based markdown rendering for your CLI apps 💇🏻‍♀️

Glamour Write handsome command-line tools with Glamour. glamour lets you render markdown documents & templates on ANSI compatible terminals. You can c

Charm 1.3k Aug 10, 2022
Your dev tool to manage /etc/hosts like a pro!

hostctl This tool gives you more control over the use of your hosts file. You can have multiple profiles and switch them on/off as you need. Why? It i

Gustavo 796 Aug 12, 2022
Emojictl - manage your emojis!

Emojictl (pronounced emoji control) is a package (& CLI wrapper) for managing Emojis on platforms that support custom emojis. Currently only Slack support is implemented.

George Kontridze 4 Mar 4, 2022
Schedule daily tweets from markdown files in your repo, posted via github actions.

markdown-tweet-scheduler Schedule daily tweets from markdown files in your repo, posted to twitter via github actions. Setup Fork this repo Get your t

reid j sherman 55 Jul 25, 2022
Convert your markdown files to PDF instantly

Will take a markdown file as input and then create a PDF file with the markdown formatting.

Arnab Sen 26 Jul 5, 2022
Colored RSS feeds in your console

RSS Console Feed Read colored rss feeds in your console Usage ./rss-console-feed

Joe Gasewicz 0 Dec 22, 2021
This service will help you detect any waste of resources in your AWS account

Frugal-Hero This service will help you detect any waste of resources in your AWS account. The policy is: if it is not useful, delete it! Requirements

null 3 Jan 31, 2022
[Crawler/Scraper for Golang]🕷A lightweight distributed friendly Golang crawler framework.一个轻量的分布式友好的 Golang 爬虫框架。

Goribot 一个分布式友好的轻量的 Golang 爬虫框架。 完整文档 | Document !! Warning !! Goribot 已经被迁移到 Gospider|github.com/zhshch2002/gospider。修复了一些调度问题并分离了网络请求部分到另一个仓库。此仓库会继续

null 208 Jul 4, 2022
golang 在线预览word,excel,pdf,MarkDown(Online Preview Word,Excel,PPT,PDF,Image by Golang)

Go View File 在线体验地址 http://39.97.98.75:8082/view/upload (不会经常更新,保留最基本的预览功能。服务器配置较低,如果出现链接超时请等待几秒刷新重试,或者换Chrome) 目前已经完成 docker部署 (不用为运行环境烦恼) Wor

CZC 69 Aug 18, 2022
bluemonday: a fast golang HTML sanitizer (inspired by the OWASP Java HTML Sanitizer) to scrub user generated content of XSS

bluemonday bluemonday is a HTML sanitizer implemented in Go. It is fast and highly configurable. bluemonday takes untrusted user generated content as

Microcosm 2.4k Aug 13, 2022
Elegant Scraper and Crawler Framework for Golang

Colly Lightning Fast and Elegant Scraping Framework for Gophers Colly provides a clean interface to write any kind of crawler/scraper/spider. With Col

Colly 17.2k Aug 9, 2022
A golang package to work with Decentralized Identifiers (DIDs)

did did is a Go package that provides tools to work with Decentralized Identifiers (DIDs). Install go get github.com/ockam-network/did Example packag

Ockam 64 Aug 3, 2022
wcwidth for golang

go-runewidth Provides functions to get fixed width of the character or string. Usage runewidth.StringWidth("つのだ☆HIRO") == 12 Author Yasuhiro Matsumoto

mattn 474 Aug 8, 2022