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:
A2: Rid the illegal string in def column of the double quote:
A3: Get the record where def is problem:
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL