Giter Site home page Giter Site logo

go-csv's Introduction

CSV

Build Status Go Report Card GoDoc

A Go CSV implementation inspired by Python's CSV module. It supports various CSV dialects (see below) and is fully backward compatible with the encoding/csv package in the Go standard library.

Examples

Here's a basic writing example:

f, err := os.Create("output.csv")
checkError(err)
defer func() {
  err := f.Close()
  checkError(err)
}
w := NewWriter(f)
w.Write([]string{
  "a",
  "b",
  "c",
})
w.Flush()
// output.csv will now contains the line "a b c" with a trailing newline.

Here's a basic reading example:

f, err := os.Open('myfile.csv')
checkError(err)
defer func() {
  err := f.Close()
  checkError(err)
}

r := NewReader(f)
for {
  fields, err := r.Read()
  if err == io.EOF {
    break
  }
  checkOtherErrors(err)
  handleFields(fields)
}

CSV dialects

To modify CSV dialect, have a look at csv.Dialect, csv.NewDialectWriter(...) and csv.NewDialectReader(...). It supports changing:

  • separator/delimiter.
  • quoting modes:
    • Always quote.
    • Never quote.
    • Quote when needed (minimal quoting).
    • Quote all non-numerical fields.
    • Quote all non-empty, non-numerical fields.
  • line terminator.
  • how quote character escaping should be done - using double escape, or using a custom escape character.

Have a look at the documentation in csv_test.go for example on how to use these. All values above have sane defaults (that makes the module behave the same as the csv module in the Go standard library).

Documentation

Package documentation can be found here.

Why was this developed?

I needed it for mysqlcsvdump to support variations of CSV output. The csv module in the Go (1.2) standard library was inadequate as it it does not support any CSV dialect modifications except changing separator and partially line termination.

Who developed this?

I'm Jens Rantil. Have a look at my blog for more info on what I'm working on.

go-csv's People

Contributors

jensrantil avatar timotto avatar

Watchers

James Cloos avatar  avatar

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.