1
votes

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

2
Please write your file reading code so that we can modify themBhavik Ambani
Can you describe a bit more the behavior you are seeing, or give a sample input/output?The Cat
I am not sure how opencsv operates. But when you don't want to manually do this, you can read the file as a preprocess and do the data massaging and then feed that stringreader to your opencsv. This is a clean method of doing it, but when you have a limitation then you got to work around it.muruga
I have added more info and code sampleuser373201

2 Answers

2
votes

Ended up using javacsv. Does not have any issues

1
votes

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.