Guest User

Untitled

a guest
Nov 16th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. def sql = Sql.newInstance("jdbc:mysql://localhost:3306/mydb",
  2. "user", "pswd", "com.mysql.jdbc.Driver")
  3. def people = sql.dataSet("PERSON")
  4. new File("users.csv").splitEachLine(",") {fields ->
  5. people.add(
  6. first_name: fields[0],
  7. last_name: fields[1],
  8. email: fields[2]
  9. )
  10. }
  11.  
  12. @Grab('com.xlson.groovycsv:groovycsv:1.1')
  13. import static com.xlson.groovycsv.CsvParser.parseCsv
  14.  
  15. for(line in parseCsv(new FileReader('countries.csv'), separator: ';')) {
  16. println "Country=$line.COUNTRY, Capital=$line.CAPITAL"
  17. }
  18.  
  19. @Grab('org.apache.commons:commons-csv:1.2')
  20. import org.apache.commons.csv.CSVParser
  21. import static org.apache.commons.csv.CSVFormat.*
  22.  
  23. import java.nio.file.Paths
  24.  
  25. Paths.get('countryInfo.txt').withReader { reader ->
  26. CSVParser csv = new CSVParser(reader, DEFAULT.withHeader())
  27.  
  28. for (record in csv.iterator()) {
  29. println record.dump()
  30. }
  31. }
  32.  
  33. @Grab('com.xlson.groovycsv:groovycsv:0.2')
  34. import com.xlson.groovycsv.CsvParser
  35.  
  36. def csv = '''Name,Lastname
  37. Mark,Andersson
  38. Pete,Hansen'''
  39.  
  40. def data = new CsvParser().parse(csv)
  41. for(line in data) {
  42. println "$line.Name $line.Lastname"
  43. }
  44.  
  45. File detailedStatsFile = new File("stats.csv");
  46. detailedStatsFile.eachLine { line, number ->
  47. // Number Of Executions, Total Milliseconds, Milliseconds per execution, "Type"
  48. def match = line =~ /([^,]*?),s*([^,]*?),s*([^,]*?),s*(?:([^",]+)|(?:"((?:[^\"]++(?:\")?)++)"))$/; //"
  49.  
  50. if (!match.matches())
  51. continue;
  52.  
  53. def numberOfExecs = Integer.valueOf(match.group(1));
  54. def totalMillis = Integer.valueOf(match.group(2));
  55. def detailedStatName = match.group(4);
  56. if (detailedStatName == null)
  57. detailedStatName = match.group(5).replaceAll('\"','"');
  58.  
  59. @Grab('com.opencsv:opencsv:4.0')
  60. class TestCsvReader {
  61.  
  62.  
  63. static main(args) {
  64. def csv = '''"a","b","c"
  65. "d","e","f"
  66. '''
  67. def Reader csvFileReader = new StringReader(csv)
  68. def Writer csvFileWriter = new PrintWriter(System.out)
  69. def CSVReader reader = new CSVReader(csvFileReader)
  70. def CSVWriter writer = new CSVWriter(csvFileWriter)
  71.  
  72. reader.iterator().each { fields ->
  73. writer.writeNext(fields)
  74. }
  75. reader.close()
  76. writer.close()
  77. }
  78. }
Add Comment
Please, Sign In to add comment