user373201
user373201

Reputation: 11425

opencsv escape single and double quotes

I am using opencsv to read a csv file. Some time the csv files have single or double quotes in them. How do i read them without changing the data in the csv file itself.

Right now if I replace a single quote with two single quotes it works fine, same with double quotes, replace a single one with two and it works. But I do not want to touch the source file.

The code to access is as following

CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), delimiter,'\"',0);
data = a1;b1;c1;hello"world;d1;e1
       a2;b3;c2;hello"world;d2;e2
; is the delimiter

The result of this is the next row which also contains a double quote is skipped, All odd numbered rows are inserted where as the even numbered rows are skipped

Thanks in advance

Upvotes: 1

Views: 3322

Answers (2)

Bhavesh
Bhavesh

Reputation: 940

Solution 1 : One possible thing you could do, Assuming you don't want double course/single courses in your output file.

  1. Read the complete file and replace all the unwanted special characters with space. (this is simple using regex)
  2. Now the read the file using CSVReader.

Solution 2: If you want to keep all the special character then put a "\" backlash before special characters. then read it.

Upvotes: 1

user373201
user373201

Reputation: 11425

Ended up using javacsv. Does not have any issues

Upvotes: 2

Related Questions