Skip to content

Instantly share code, notes, and snippets.

@pditommaso
Forked from crazy4groovy/csv2List.groovy
Created January 16, 2014 21:27
Show Gist options
  • Save pditommaso/8463735 to your computer and use it in GitHub Desktop.
Save pditommaso/8463735 to your computer and use it in GitHub Desktop.
@Grab('com.xlson.groovycsv:groovycsv:1.0')
import com.xlson.groovycsv.CsvParser
// a normally parsed csv can only be searched through ONCE
// this is used to create a "re-searchable" object (List of Maps)
List<Map> csv2List( String filePath, boolean shouldTrim = true ) {
new File( filePath ).withReader { r ->
new CsvParser().parse( r ).with { csv ->
return csv.collect { line ->
line.columns.collectEntries { c, v ->
[ c, (shouldTrim ? line[ c ].trim() : line[ c ]) ]
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment