【问题】
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文件的内容并去掉字符串两端的引号,结果如下:

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

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

在处理CSV文件时遇到含有额外双引号的问题导致程序失败。解决方案包括使用SPL库来读取并修正包含特殊字符的CSV内容。
686

被折叠的 条评论
为什么被折叠?



