Java OpenCSV CSVParser delimiter inside quoted string

1k Views Asked by At

I have an issue to parse a CSV file with the following separator ';' and quote char '"'.

The problem is that I have to deal with quotes and semicolons inside the quoted string.

I cannot change the source file / separators etc.

I am using the library com.opencsv version 5.6 to parse the CSV.

CSV file:

"Col1";"Col2";"Co"l3""";"Col; Col"

Expected parsing result:

  1. Position 1: Col1
  2. Position 2: Col2
  3. Position 3: Co"l3"
  4. Position 4: Col; Col

This is the parser:

CSVParser parser = new CSVParserBuilder()
            .withSeparator(';')
            .withQuoteChar('"')
            .withIgnoreQuotations(true)
            .build();

This is result:

  1. (ok) Position 1: Col1
  2. (ok) Position 2: Col2
  3. (ok) Position 3: Co"l3"
  4. (error) Position 4: Col
  5. (error) Position 5: Col

The parser is treating well the quotes inside the quoted string, but is wrongly splitting the last element.

P.S. : If I don't use "with Ignore Quotations" an exception is thrown.

Could you please help me on solve this issue?

Thanks

0

There are 0 best solutions below