处理含有特殊字符的 csv

【问题】

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.

【回答】

       如果无法用opencsv解决,可以试试SPL:

A
1 file("D:\\data.csv").import@tcq()=
2 =A1.run(replace(def,"\"",""):def)
3 =A2.select(def=="problem")

A1:读取csv文件的内容并去掉字符串两端的引号,结果如下:

处理含有特殊字符的 csv

A2:去掉def列那个不合法字符串中的引号:

处理含有特殊字符的 csv

A3:查询def列中等于problem的数据,结果如下:

处理含有特殊字符的 csv

上一篇:A Simple Problem with Integers (线段树)


下一篇:C++ 学习笔记(三):介绍几个在线编译器【转】