@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 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 ]) ]
}
}
}
}
}
Showing posts with label csv. Show all posts
Showing posts with label csv. Show all posts
Wednesday, January 15, 2014
csv-2-List for groovycsv
Monday, December 5, 2011
CSV file parser I/O
@Grab(group = 'net.sf.opencsv', module = 'opencsv', version = '2.3')
import au.com.bytecode.opencsv.CSVReader
import au.com.bytecode.opencsv.CSVParser
import au.com.bytecode.opencsv.CSVWriter
String TEST_FILE_NAME = 'C:\\test.csv'
String TEST_OUTPUT_FILE_NAME = 'C:\\testOut.csv'
List< String[] > rows = new CSVReader(
new FileReader(new File(TEST_FILE_NAME)),
CSVParser.DEFAULT_SEPARATOR,
CSVParser.DEFAULT_ESCAPE_CHARACTER,
CSVParser.DEFAULT_QUOTE_CHARACTER, 1
).readAll()
List< String[] > rowsOver100 = rows.findAll { it[1].toInteger() > 100 }
File output = new File(TEST_OUTPUT_FILE_NAME)
if (output.exists()) {
output.delete()
}
output.createNewFile()
output.withWriter { writer ->
new CSVWriter(writer).writeAll(rowsOver100)
}
// for streaming, see also: http://www.groovy-tutorial.org/basic-csv/
Subscribe to:
Comments (Atom)