groovy

Groovy add column value in csv file


I need to export JSON in csv file. how to add values ​​to each column? I know how to add rows. Here's an example code:

def data = [
        ['place', 'firstname', 'lastname', 'team'],
        ['1', 'Lorena', 'Wiebes', 'Team DSM'],
        ['2', 'Marianne', 'Vos', 'Team Jumbo Visma'],
        ['3', 'Lotte', 'Kopecky', 'Team SD Worx']
]
def file = new File("C:/Temp/data.csv")
file.text = data*.join(',').join(System.lineSeparator())

Result:

enter image description here

I need result:

enter image description here

Please suggest good literature on Groovy with examples, thank you.


Solution

  • def data = [
            ['place', 'firstname', 'lastname', 'team'],
            ['1', 'Lorena', 'Wiebes', 'Team DSM'],
            ['2', 'Marianne', 'Vos', 'Team Jumbo Visma'],
            ['3', 'Lotte', 'Kopecky', 'Team SD Worx']
    ]
        def file = new File("C:/Temp/data.csv")
    
        def stringBuilder = new StringBuilder()
        data.each { row ->
        stringBuilder.append(row.join(';')).append(System.lineSeparator())
        }
    
        file.text = stringBuilder.toString()