Posted By

roock on 02/18/08


Tagged

list format csv


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

melling


CSV formatter


 / Published in: Groovy
 

Format a list of lists to CSV format

  1. class CsvFormat {
  2.  
  3. def convertListToCsv(list) {
  4. def result = ''
  5. list.each{row ->
  6. row.each{col -> result += col + ','}
  7. result = result[0..-2]
  8. result += '\n'
  9. }
  10. result
  11. }
  12.  
  13. }
  14.  
  15.  
  16.  
  17. class CsvFormatTest extends GroovyTestCase {
  18.  
  19. void testConvertsListToCsv() {
  20. assertEquals '1,2\n3,4\n', new CsvFormat().convertListToCsv([[1,2],[3,4]])
  21. }
  22.  
  23. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: mycrobe on February 18, 2009

better:

`class CsvFormat {

def convertListToCsv(list) {
    def result = new StringBuilder()
    list.each{row ->
        result
Posted By: mycrobe on February 18, 2009

sigh... learning markdown. Anyway, this is faster for larger datasets

class CsvFormat {

def convertListToCsv(list) {
    def result = new StringBuilder()
    list.each{row ->
        result
Posted By: mycrobe on February 18, 2009

class CsvFormat { def convertListToCsv(list) { def result = new StringBuilder() list.each{row -> result

Posted By: mycrobe on February 18, 2009

fucking markdown :-P

You need to login to post a comment.