Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwanted double quotes in generated csv file

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.

like image 580
Edward Avatar asked Dec 20 '12 09:12

Edward


People also ask

How do I fix a double quote in a CSV file?

There are 2 accepted ways of escaping double-quotes in a CSV file. One is using a 2 consecutive double-quotes to denote 1 literal double-quote in the data. The alternative is using a backslash and a single double-quote.

Why does my CSV have double quotes?

Since CSV files use the comma character "," to separate columns, values that contain commas must be handled as a special case. These fields are wrapped within double quotation marks. The first double quote signifies the beginning of the column data, and the last double quote marks the end.

How do you remove quotes from CSV?

csv file. Use the Foreach-Object cmdlet (% is an alias) to read each line as it comes from the file. Inside the script block for the Foreach-Object command, use the $_ automatic variable to reference the current line and the replace operator to replace a quotation mark with nothing.


2 Answers

This worked for me

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

See the CSVWriter javadoc

like image 151
Shamis Shukoor Avatar answered Oct 16 '22 19:10

Shamis Shukoor


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" 
like image 29
James Bassett Avatar answered Oct 16 '22 20:10

James Bassett