Search code examples
csvgrailsgroovygrails-plugin

How to read a backslash as a special character in Grails CSV Plugin


i'm reading a csv file in which each properties are sperated by using pipes(|). One of the property value in the csv file is something like this '123\56' while reading this using toCsvReader getting the value as '12356'. if the property value is given as '123\56'it will read it as '123\56'.

how to read a single backslash as a single special character?

inputFileContent.toCsvReader([separatorChar: '|']).eachLine { tokens ->

}

Solution

  • According to the documentation you can change the escapeChar from the default of backslash using the configuration options.

    inputFileContent.toCsvReader([separatorChar: '|', escapeChar: '']).eachLine { tokens ->
      ...
    }
    

    The above will remove the escapeChar and treat backslash as just a normal character.