Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/wrg/csvsplit

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/wrg/csvsplit

  • v0.0.0-20130315141018-b8f898bf3aa3
  • Source
  • Go
  • Socket score

Version published
Created
Source

csvsplit - lightweight csv string splitter.

Version

csvsplit 1.0

Synopsis

Package csvsplit is a simple csv line parser that properly handles quotes and nested quotes (i.e. 'text, "quoted", with commas'). This does not trim spaces or newline characters, and only returns string values. Conversion or manipulation is not assumed and left to the developer. The field delimiter is a comma (,) and the quote characters are single and double quotes.

Example

package main

import (
   "io"
   "bufio"
   "fmt"
   "os"
   "github.com/wrg/csvsplit"
)

func main() {
   file, err := os.Open(os.Args[1])
   if err != nil { panic(err) }
   defer file.Close()
   r := bufio.NewReader(file)
   for {
       line, err := r.ReadString('\n')
       if err != nil && err != io.EOF { panic(err) }
       if line != "" {
         // remove newline chars yourself!
         if line[len(line)-1] == '\n' {
            line = line[:len(line)-1]
         }
         fields, splitErr := csvsplit.Split(line)
         if splitErr != nil {
            fmt.Println(splitErr)
         }
         fmt.Println(fields)
       }
       if err == io.EOF { break }
   }
}

Given a CSV file 'example.csv' with contents

Bob,28,'text, "quoted", with commas',3/13/2013
Rick,44,"text, 'inverse' commas",3/12/2013
John,68,No Comment,1/1/1970
Jake,18,,2/2/2000

When the above program is called and given the path 'example.csv', the following is printed to standard output.

[Bob 28 'text, "quoted", with commas' 3/13/2013]
[Rick 44 "text, 'inverse' commas" 3/12/2013]
[John 68 No Comment 1/1/1970]
[Jake 18  2/2/2000]

About

csvsplit is a lightweight string parser that will split the input text into
an array of strings.  It is not intended to be a full blown file parser, and
does not do any type conversion.

Features

* Properly handles quotes and nested quotes

* Lightweight - no unwanted code imported into your application

Todo

* Cannot change the field separator, would like to make that an option in a future release.

Install

The easiest installation of csvsplit is done through go get.

go get github.com/wrg/csvsplit

Documentation

The best way to read the current csvsplit documentation is using godoc.

godoc github.com/wrg/csvsplit

Or better yet, you can run a godoc http server.

godoc -http=":6060"

Then go to the url http://localhost:6060/pkg/github.com/x86pgmer/csvsplit/

Copyright (c) 2013, Rick Gibson. All rights reserved.

Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

FAQs

Package last updated on 15 Mar 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc