I am using OpenCSV.
I have a CSVReader
trying to parse a CSV file.
That file has quote char "
and separator char ,
and escape char also "
.
Note that the CSV contains cells like:
"ballet 24"" classes"
"\"
which actually represent these values:
ballet 24" classes
\
Example:
"9/6/2014","3170168","123652278","Computer","2329043290","Bing and Yahoo! search","22951990789","voice lesson","Broad","0.00","0","1","3.00","0.00","0.00","0.00","7","0","",""
"9/6/2014","3170168","123652278","Smartphone","2329043291","Bing and Yahoo! search","22951990795","ballet class","Broad","0.00","0","1","1.00","0.00","0.00","0.00","0","0","",""
"9/6/2014","3170168","123652278","Smartphone","2329043291","Bing and Yahoo! search","22951990797","ballet 24"" classes","Broad","0.00","0","1","1.00","0.00","0.00","0.00","0","0","",""
"9/6/2014","3170168","123652278","Smartphone","2329043291","Bing and Yahoo! search","22951990797","ballet classes","Broad","0.00","0","1","1.00","0.00","0.00","0.00","0","0","",""
"9/6/2014","3170168","123652278","Computer","2329043291","Bing and Yahoo! search","22951990817","\","Broad","0.00","0","1","1.00","0.00","0.00","0.00","5","0","",""
"9/6/2014","3170168","123652278","Computer","2329043293","Bing and Yahoo! search","22951990850","zumba classes","Broad","0.00","0","1","7.00","0.00","0.00","0.00","5","0","",""
"9/6/2014","3170168","123652278","Smartphone","2329043293","Bing and Yahoo! search","22951990850","zumba classes","Broad","0.00","0","4","1.00","0.00","0.00","0.00","5","0","",""
"9/6/2014","3170168","123652278","Computer","2329043293","Bing and Yahoo! search","22951990874","zumba lessons","Broad","0.00","0","1","2.00","0.00","0.00","0.00","0","0","",""
My problem is that I cannot specify "
for escape char to the CSVReader
constructor
(i.e. make it the same as the quote char).
If I do so, the CSVReader
simply goes crazy, and it reads the whole CSV line as a single CSV cell.
Has anyone else encountered this bug and how to get around it?!
It will work if you go with the default settings for CsvReader.
Check this open bug they have: sourceforge.net/p/opencsv/bugs/83:
By default, it is able to escape double quote with double quote, but your 'true' escape character must still be something else.
So the following works:
At first I put '\' as escape character, but then, your field "\" would need to be modified to escape the escape character.
The
CSVReader
is not fully RFC4180 compliant. Use their newer CSV reader (RFC4180Parser):To read a String line formatted as a CSV:
To use the reader (an alternative is
reader.readNext()
):See http://opencsv.sourceforge.net/#rfc4180parser for more details.
Code taken from GeekPrompt
It cannot be done through CSVReader