0

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

Jason Plurad
  • 6,682
  • 2
  • 18
  • 37
Pratiyush Kumar Singh
  • 1,977
  • 3
  • 19
  • 39

1 Answers1

1

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().

Jason Plurad
  • 6,682
  • 2
  • 18
  • 37
  • 1
    Also consider posting your question on their user mailing list. Many Apache projects use them more often than SO. https://commons.apache.org/proper/commons-csv/mail-lists.html – Jason Plurad Apr 16 '16 at 15:33