Fork me on GitHub

GroovyCSV by xlson

A simple CSV parsing library for groovy

GroovyCSV is a library to make csv processing just a little bit Groovier. The library uses opencsv behind the scenes and merely tries to add a thin layer of “Groovy-ness” to the mix.

Latest version

1.0

Dependencies

opencsv 2.1

Documentation and more information

Features

Arguments to parse or parseCsv

Usage example

Basic usage

@Grab('com.xlson.groovycsv:groovycsv:1.0')
import static com.xlson.groovycsv.CsvParser.parseCsv
def csv = '''ID,Product
3,Shoe
1,Table'''

def data = parseCsv(csv)
for(line in data) {
    println "ID=$line.ID, Product=$line.Product"
}

Output:

ID=3, Product=Shoe
ID=1, Product=Table

Custom separator and quote character

@Grab('com.xlson.groovycsv:groovycsv:1.0')
import com.xlson.groovycsv.CsvParser
def csv = '''Name-Lastname
Mark-'Anderson-Nielsen'
Pete-Hansen'''

def data = new CsvParser().parse(csv, separator: '-', quoteChar: "'")
for(line in data) {
    println "$line.Name $line.Lastname"
}

Output:

Mark Andersson-Nielsen
Pete Hansen

Parsing a headerless file

@Grab('com.xlson.groovycsv:groovycsv:1.0')
import com.xlson.groovycsv.CsvParser
def csv = '''Apple,2
Pear,5'''

def data = new CsvParser().parse(csv, readFirstLine:true,
                                 columnNames:['fruit', 'qty'])
for(line in data) {
    println "$line.fruit ${line[1]}"
}

Output:

Apple 2
Pear 5

License

Apache V2

Authors

Leonard Axelsson (leo@xlson.com)

Downloads

GroovyCSV 1.0

GroovyCSV 0.2

You can also clone the project with Git by running: $ git clone git://github.com/xlson/groovycsv