Search code examples
scalacsvunivocity

CsvParser not working for missing double quotes


I have a messages in file like below and I am using com.univocity.parsers.csv.CsvParser to split the string based on delimiter(in this case its -)

1-bc-"name"-def-address

1-abc-"name-def-address

I create my CsvParser object like

private val settings = new CsvParserSettings()
settings.getFormat.setDelimiter('-')
settings.setIgnoreLeadingWhitespaces(true)
settings.setIgnoreTrailingWhitespaces(true)
settings.setReadInputOnSeparateThread(false)
settings.setNullValue("")
settings.setMaxCharsPerColumn(-1)
val parser = new CsvParser(settings)

and parse the input message like :

    for (line <- Source.fromFile("path\\test.txt").getLines) {
  println(parser.parseLine(line).toList)
}

and the output is:

List(1, bc, name, def, address)
List(1, abc, name-def-address)

If you see the output you can see that for 1st message the string was split properly however for second message it takes everything as a value after first double quote. Does anyone know why the behavior is like this and how can I get the desired output? I am reading every message as a string to it should simple treat a quote/double quote as a character.


Solution

  • Author of this library here. When the quote is found after your - delimiter, the parser will try to find a closing quote.

    The easiest way around this is to make the parser simply ignore quotes with:

    settings.getFormat().setQuote('\0');
    

    Hope it helps.