Unwanted double quotes in generated csv file

49,827

Solution 1

This worked for me

CSVWriter writer = 
    new CSVWriter(new FileWriter(csv), ',', CSVWriter.NO_QUOTE_CHARACTER);

See the CSVWriter javadoc

Solution 2

You should probably clarify what you mean by 'unwanted' quotes.

  1. I don't want it to quote everything, only the fields that contain embedded commas, quotes and newlines (quoting everything is unnecessary and makes my files bigger), or

  2. I don't want anything quoted, and I understand that my CSV will be invalid if it contains embedded commas, quotes and newlines

If it's the first option, then opencsv doesn't support this - it either quotes everything or nothing. Take a look at Super CSV if you want an open source CSV library that only quotes when necessary (and can quote everything too, if required).

If it's the second option, then go with Sheldon's answer, but just be aware the your CSV will be invalid if it contains embedded commas, quotes and newlines.

For example, if I'm reading your CSV file, how am I meant to know that the following is actually just a single record with 2 fields?

P Sherman, 42 Wallaby Way,
Sydney, AUSTRALIA

Whereas if it was quoted properly that would be obvious, i.e.

P Sherman, "42 Wallaby Way,
Sydney, AUSTRALIA"

FYI, here's the rules relating to quotes from RFC4180 (the MIME type definition for CSV).

5 Each field may or may not be enclosed in double quotes (however some programs, such as Microsoft Excel, do not use double quotes at all). If fields are not enclosed with double quotes, then double quotes may not appear inside the fields. For example:

   "aaa","bbb","ccc" CRLF
   zzz,yyy,xxx

6 Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. For example:

   "aaa","b CRLF
   bb","ccc" CRLF
   zzz,yyy,xxx

7 If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:

   "aaa","b""bb","ccc"

Solution 3

If you don't want the quotes in the values of the generated CSV file, you have to create the CSVWriter object in this way:

CSVWriter writer = new CSVWriter(new FileWriter(filePath),
    CSVWriter.DEFAULT_SEPARATOR,
    CSVWriter.NO_QUOTE_CHARACTER,
    CSVWriter.DEFAULT_ESCAPE_CHARACTER,
    CSVWriter.RFC4180_LINE_END);

The key is CSVWriter.NO_QUOTE_CHARACTER. You can customize the values of the other constructor parameters.

Solution 4

private void writeFile(String fileAbsolutePath , ListcsvLines) throws IOException{

    final char csvDelimeter = ',';
    CSVWriter csvWriter = new CSVWriter(new FileWriter(new File(fileAbsolutePath)),csvDelimeter,CSVWriter
                                                                                               .NO_QUOTE_CHARACTER);
    CSVParser parser = new CSVParser();

    for(String csvLine  : csvLines){
        String[] csvVals = parser.parseLine(csvLine);
        csvWriter.writeNext(csvVals);
    }
    csvWriter.flush();
}

Call : writeFile(fileAbsolutePath,csvLinesList);

Working example for Shamis's answer ,It works fine for me.

Share:
49,827
Edward
Author by

Edward

Updated on June 01, 2021

Comments

  • Edward
    Edward almost 3 years

    I have created a CSV file using the Java code below:

    String csv = rs.getString("UPLOAD_FOLDER_PATH")+".csv";
    CSVWriter writer = new CSVWriter(new FileWriter(csv));
    String [] filevalues = new String[filevaluesarray.size()];
    
    filevalues=filevaluesarray.toArray(filevalues);
    
    writer.writeNext(filevalues);
    
    writer.close();
    

    I am getting the CSV file, but the content of the file has unwanted double quotes.

    Eg. "ABC","123","KDNJ"

    I don't get from where these double quotes are added.