wsn
wsn

Reputation: 49

Trying to split each line by comma Groovy

I'm trying to do some tests in groovy for integration in my pipeline. Basically what I want is to split each line of the csv folder by commas. Imagine I have in file1.csv this content: Rice,true,Good Pasta,false,Bad Chicken,true,Ok With my groovy method, I want to split the csv by commas and have a list of strings of the food. But when I'm trying to print to see if the split is happening I get an error message.

Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'D:\Desktop\test\file1.csv' with class 'java.lang.String' to class 'groovy.lang.GString'

Can someone help me out understand what I'm doing wrong? Thanks :)

def test() {
    def mapParts = [:]
    readFile("D:\\Desktop\\test\\file1.csv" as GString).splitEachLine( /,/ )
            { it ->
        println it
    }
}

Upvotes: 0

Views: 1439

Answers (1)

injecteer
injecteer

Reputation: 20699

This should work in plain Groovy:

new File("D:\\Desktop\\test\\file1.csv").splitEachLine( /,/ ){ 
  println it 
}

The option with readFile as per ref-doc and this example requires a map:

readFile(file: "D:\\Desktop\\test\\file1.csv").splitEachLine( /,/ ){ 
  println it 
}

Upvotes: 1

Related Questions