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.
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.
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.
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.
This worked for me
CSVWriter writer = new CSVWriter(new FileWriter(csv), ',', CSVWriter.NO_QUOTE_CHARACTER);
See the CSVWriter javadoc
You should probably clarify what you mean by 'unwanted' quotes.
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
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With