Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active March 9, 2021 20:50
Show Gist options
  • Save crazy4groovy/8438827 to your computer and use it in GitHub Desktop.
Save crazy4groovy/8438827 to your computer and use it in GitHub Desktop.
A Groovy script that takes a CSV filePath (string), and turns it into a List of Maps (via groovycsv lib).
@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, String charset = 'UTF-8' ) {
new File( filePath ).withReader(charset) { r ->
new CsvParser().parse( r ).with { csv ->
return csv.collect { line ->
line.columns.collectEntries { c, v ->
[ c, (shouldTrim ? line[ c ].trim() : line[ c ]) ]
}
}
}
}
}
@marcusvoltolim
Copy link

      new CsvParser().parse(r).with { csv ->
                csv.collect { PropertyMapper line ->
                    line.toMap()
                }
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment