csv Module

csv module provides functions to parse/encode CSV files.

Reading CSV Files

readCSV( file { , columnNames } )

readCSV() returns a Generator object from which tokens can be retrieved. When non-null columnNames is given, the tokens are represented as a Map object that each token is associated with a column name. Otherwise, a the tokens are represented as a List object.

for (columns: readCSV("test.csv")){
    println(columns[0], ":", columns[1])
}
for (person: readCSV("test.csv", ["name", "age"])){
    println(person.name, ", ", person.age)
}

Encoding as CSV Columns

encodeCSV( string { , boolean quoteAsNeeded } )

encodeCSV() encodes a given string to quoted CSV column string. If quoteAsNeeded is true, the given string is quoted only when either double quote, comma, or new line characters is included in the string.

encodeCSV("\\"") --> """
encodeCSV("1-2-3", true) --> 1-2-3