Groovy Documentation

com.xlson.groovycsv
[Groovy] Class CsvParser

java.lang.Object
  com.xlson.groovycsv.CsvParser

class CsvParser

Helper class used to parse information from csv files using the column names in the first line. Currently it only supports csv files where the first line contains the column names.

Usage:

 def csv = '''Name,Lastname
 Mark,Andersson
 Pete,Hansen'''

 def data = new CsvParser().parse(csv)
 for(line in data) {
   println "$line.Name $line.Lastname"
 }

Parser configuration

There parser is configurable using named arguments. These arguments are available.

Usage:

 def csv = '''Fruit-Quantity
 Apple-2
 Pear-5'''

 def data = new CsvParser().parse(csv, separator: '-')

 // Print all fruits that have a quantity higher than 3
 data.findAll{ (it.Quantity as int) > 3 }.each{ println it }
 

Authors:
Leonard Axelsson
Since:
0.1


Method Summary
java.lang.Object parse(java.util.Map args = [:], java.lang.String csv)

Parses the csv supplied using the reader.

java.lang.Object parse(java.util.Map args = [:], java.io.Reader reader)

Parses the supplied csv and returns a CsvIterator that can be use to access the data.

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Method Detail

parse

java.lang.Object parse(java.util.Map args = [:], java.lang.String csv)
Parses the csv supplied using the reader. See parse(Reader reader) for more information about usage.
Parameters:
args - configurable parameters
csv - the csv to parse
Returns:
an instance of com.xlson.groovycsv.CsvIterator


parse

java.lang.Object parse(java.util.Map args = [:], java.io.Reader reader)
Parses the supplied csv and returns a CsvIterator that can be use to access the data. The first line of the csv will be used as column-headers. Named paramenters can be used to configure the parsing, see the class documentation for more more information on usage.

Arguments for configuration:

  • separator : Sets a custom separator like tab
  • quoteChar : Sets a custom quote character
  • escapeChar : Sets a custom escape character for the separator and quoteChar
    Parameters:
    reader - the csv to parse
    args - the configuration arguments
    Returns:
    an instance of com.xlson.groovycsv.CsvIterator

  •  

    Groovy Documentation