Groovy Documentation

com.xlson.groovycsv
[Groovy] Class CsvParser

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

class CsvParser
extends java.lang.Object

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"
 }
Authors:
Leonard Axelsson
Since:
0.1


Property Summary
java.lang.Integer autoDetectCharNumber

Number of characters used to provide to autodetection (in case auto detection is used.

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

Parses the csv supplied using the reader.

java.util.Iterator 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.

static java.util.Iterator parseCsv(java.util.Map args = [:], java.lang.String csv)

Parses a string as csv in the same way as CsvParser.parse(...).

static java.util.Iterator parseCsv(java.util.Map args = [:], java.io.Reader reader)

Parses a reader as csv in the same way as CsvParser.parse(...).

 
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()
 

Property Detail

autoDetectCharNumber

java.lang.Integer autoDetectCharNumber
Number of characters used to provide to autodetection (in case auto detection is used.


 
Method Detail

parse

java.util.Iterator 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.util.Iterator 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. There's also support for autodetecting the quote and separator characters.

Arguments for configuration:

  • separator: configures the separator character to use (default: ,)
  • quoteChar: configures the quote character to use (default: ")
  • escapeChar: configures the escape character for the separator and quoteChar (default:\
  • autoDetect: sets up autodetect that will honor other configurations you've done (default: false)
  • columnNames: set custom column names instead of using the first line
  • readFirstLine: reads the first line as csv instead of using it as headers

    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 }
     

    Parameters:
    reader - the csv to parse
    args - the configuration arguments
    Returns:
    an instance of com.xlson.groovycsv.CsvIterator

  • parseCsv

    static java.util.Iterator parseCsv(java.util.Map args = [:], java.lang.String csv)
    Parses a string as csv in the same way as CsvParser.parse(...).
    Parameters:
    args
    csv
    Returns:


    parseCsv

    static java.util.Iterator parseCsv(java.util.Map args = [:], java.io.Reader reader)
    Parses a reader as csv in the same way as CsvParser.parse(...).
    Parameters:
    args
    csv
    Returns:


     

    Groovy Documentation