Pratiyush Kumar Singh
Pratiyush Kumar Singh

Reputation: 1997

Unexpected quoting in Apache Commons CSV

While using Apache CSV with below maven dependency. I am getting unexpected results.

  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.2</version>
</dependency>

Simple code is not working for me as expected. Can anyone tell what is wrong here?

System.out.println(CSVFormat.DEFAULT.format("-10","10"));

Actual Output: "-10",10

Expected Output: -10,10

System.out.println(CSVFormat.DEFAULT.format("10","-10"));

Actual Output: 10,-10

Expected Output: 10,-10

System.out.println(CSVFormat.DEFAULT.format(".10","-10"));

Actual Output: ".10",-10

Expected Output: .10,-10

System.out.println(CSVFormat.DEFAULT.format("+10","-10"));

Actual Output: "+10",-10

Expected Output: +10,-10

Upvotes: 0

Views: 795

Answers (1)

Jason Plurad
Jason Plurad

Reputation: 6782

The actual results you listed above are legitimate CSV format (it is parseable even with the quotes), but I can see how it can look like an unexpected output.

Here is the source code in CSVPrinter.java:

// TODO where did this rule come from?
if (newRecord && (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || (c > 'z'))) {
    quote = true;
}

This code will quote the first item in the row if it doesn't start with an alphanumeric character.

If you browse through the code history on the class, you will find that this behavior has been there since the beginning of the repository (November 9, 2011).

I didn't notice any related bugs on the issue tracker, so you should open up a issue if you think the default behavior should be changed. Alternately, you could consider using a applying a QuoteMode using CSVFormat.withQuoteMode().

Upvotes: 1

Related Questions