
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
github.com/wrg/csvsplit
csvsplit - lightweight csv string splitter.
csvsplit 1.0
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.
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]
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.
* Properly handles quotes and nested quotes
* Lightweight - no unwanted code imported into your application
* Cannot change the field separator, would like to make that an option in a future release.
The easiest installation of csvsplit is done through go get.
go get github.com/wrg/csvsplit
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
Unknown package
Did you know?
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.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.