11

I am using apache commons CSV to write csv files. I want to stick to this library. While I am writing a csv file, in the first column of generated file, it contains double quotes as quote character and other columns are generated as expected.

I really want to get rid of double quotes here. Please find below code for the same.

CSVFormat format = CSVFormat.DEFAULT;
FileWriter fw = new FileWriter("Temp.csv");
CSVPrinter printer = new CSVPrinter(fw, format);

String[] temp = new String[4];

for(int i=0; i<4; i++) {
    if(i==1)
        temp[0] = "#";
    else
        temp[0] = "";
    temp[1] = "hello" + (i+1);
    temp[2] = "";
    temp[3] = "test";

    Object[] temp1 = temp[]
    printer.printRecord(temp1);
}

fw.close();
printer.close();

Temp.csv

"",hello1,,test
"#",hello2,,test
"",hello3,,test
"",hello4,,test

I don't want a quote character at the beginning of every row. I just want an empty string without quotes, same as in column 3. Can anyone help?

1
  • I know this is 5 years old, but it is outputting an empty string in the first field. That is why there are two quotes. It's interesting as when I started working with CSV files all text fields were surrounded by quotes. Maybe I was jaded because I was mostly working in the Microsoft realm at the time. Now I've come back and seems like things generate CSV's a lot more looser. Jun 30, 2020 at 17:57

2 Answers 2

13

Mentioned in lars issue tracking, try to set the CSVFormat to the following,

final CSVFormat csvFileFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE);

1
  • tried on 1.5 version, do you know which version this works? Jan 5, 2021 at 2:09
8

This is a known issue. You can vote for it in the apache commons csv issue tracker:

https://issues.apache.org/jira/browse/CSV-63

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.