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.3

Dependencies

opencsv 4.x

Documentation and more information

Features

Arguments to parse or parseCsv

Usage example

Basic usage

@Grab('com.xlson.groovycsv:groovycsv:1.3')
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.3')
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.3')
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

The Apache License, Version 2.0

Author

Leonard Gram (leo@xlson.com)

Contributors

Downloads

GroovyCSV 1.3

GroovyCSV 1.2

GroovyCSV 1.1

GroovyCSV 1.0

GroovyCSV 0.2

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