其他分享
首页 > 其他分享> > 处理含有特殊字符的 csv

处理含有特殊字符的 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
1file("D:\\data.csv").import@tcq()=
2=A1.run(replace(def,"\"",""):def)
3=A2.select(def=="problem")

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

undefined

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

undefined

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

undefined

标签:abc,last,extra,含有,特殊字符,problem,csv,def
来源: https://blog.csdn.net/raqsoft/article/details/121739965