Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sql = Sql.newInstance("jdbc:mysql://localhost:3306/mydb",
- "user", "pswd", "com.mysql.jdbc.Driver")
- def people = sql.dataSet("PERSON")
- new File("users.csv").splitEachLine(",") {fields ->
- people.add(
- first_name: fields[0],
- last_name: fields[1],
- email: fields[2]
- )
- }
- @Grab('com.xlson.groovycsv:groovycsv:1.1')
- import static com.xlson.groovycsv.CsvParser.parseCsv
- for(line in parseCsv(new FileReader('countries.csv'), separator: ';')) {
- println "Country=$line.COUNTRY, Capital=$line.CAPITAL"
- }
- @Grab('org.apache.commons:commons-csv:1.2')
- import org.apache.commons.csv.CSVParser
- import static org.apache.commons.csv.CSVFormat.*
- import java.nio.file.Paths
- Paths.get('countryInfo.txt').withReader { reader ->
- CSVParser csv = new CSVParser(reader, DEFAULT.withHeader())
- for (record in csv.iterator()) {
- println record.dump()
- }
- }
- @Grab('com.xlson.groovycsv:groovycsv:0.2')
- import com.xlson.groovycsv.CsvParser
- def csv = '''Name,Lastname
- Mark,Andersson
- Pete,Hansen'''
- def data = new CsvParser().parse(csv)
- for(line in data) {
- println "$line.Name $line.Lastname"
- }
- File detailedStatsFile = new File("stats.csv");
- detailedStatsFile.eachLine { line, number ->
- // Number Of Executions, Total Milliseconds, Milliseconds per execution, "Type"
- def match = line =~ /([^,]*?),s*([^,]*?),s*([^,]*?),s*(?:([^",]+)|(?:"((?:[^\"]++(?:\")?)++)"))$/; //"
- if (!match.matches())
- continue;
- def numberOfExecs = Integer.valueOf(match.group(1));
- def totalMillis = Integer.valueOf(match.group(2));
- def detailedStatName = match.group(4);
- if (detailedStatName == null)
- detailedStatName = match.group(5).replaceAll('\"','"');
- @Grab('com.opencsv:opencsv:4.0')
- class TestCsvReader {
- static main(args) {
- def csv = '''"a","b","c"
- "d","e","f"
- '''
- def Reader csvFileReader = new StringReader(csv)
- def Writer csvFileWriter = new PrintWriter(System.out)
- def CSVReader reader = new CSVReader(csvFileReader)
- def CSVWriter writer = new CSVWriter(csvFileWriter)
- reader.iterator().each { fields ->
- writer.writeNext(fields)
- }
- reader.close()
- writer.close()
- }
- }
Add Comment
Please, Sign In to add comment