1
import java.io.IOException;
import java.io.StringReader;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;

I try to parse a simple csv file with Apache CSV parser. It works fine as long as I don't use quotes. When I try to add a quote to the input

"a";42

it gives me the error:

invalid char between encapsulated token and delimiter

Here is a simple, complete code:

public class Test {

    public static void main(String[] args) throws IOException {
        String DATA = "\"a\";12";
        CSVParser csvParser =    
        CSVFormat.EXCEL
            .withIgnoreEmptyLines()
            .withIgnoreHeaderCase()
            .withRecordSeparator('\n').withQuote('"')
            .withEscape('\\').withRecordSeparator(';').withTrim()
            .parse(new StringReader(DATA));
    }

}

I simply can't find out what I've missed in the code.

1 Answer 1

3

The problem was so trivial I missed it.

I used withRecordSeparator instead of withDelimiter to set the field separator.

This works as I expected:

public class Test {

    public static void main(String[] args) throws IOException {
        String DATA = "\"a\";12";
        CSVParser csvParser =    
        CSVFormat.EXCEL
            .withIgnoreEmptyLines()
            .withIgnoreHeaderCase()
            .withRecordSeparator('\n').withQuote('"')
            .withEscape('\\').withDelimeter(';').withTrim()
            .parse(new StringReader(DATA));
    }

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.