Handling CSV Containing Special Characters

Question

I have a csv file that looks like this:

"first", "abc", "def", "last"

"111", "abc", "p"roblem","last"

"aaa", "abc", "456", "last"

As you can see the second row has an extra double quote in text.

I am using below constructor:

reader = new CSVReader(new FileReader("temp.csv"), ',', '"', '|', 1);

Program fails because of that extra " in 2nd row.

How can I workaround this problem. I can't change the csv.

Thanks.

 

Answer

Try using esProc SPL to solve the problem:

A

1

=file("D:\\data.csv").import@tcq()

2

=A1.run(replace(def,"\"",""):def)

3

=A2.select(def=="problem")

 

A1: Import the csv file and remove the double quotes at the two ends of each string:

 undefined

A2: Rid the illegal string in def column of the double quote:

 undefined

A3: Get the record where def is problem:

 undefined