gpt4 book ai didi

java - CSVReader - 使用“for escape char 时的错误

转载 作者:搜寻专家 更新时间:2023-11-01 00:54:24 24 4
gpt4 key购买 nike

我正在使用 OpenCSV。

我有一个 CSVReader 试图解析 CSV 文件。
该文件包含引号字符 " 和分隔符字符 , 以及转义字符 "

请注意,CSV 包含如下单元格:

"ballet 24"" classes"
"\"

实际上代表这些值:

ballet 24" classes
\

示例:

"9/6/2014","3170168","123652278","Computer","2329043290","Bing and Yahoo! search","22951990789","voice lesson","Broad","0.00","0","1","3.00","0.00","0.00","0.00","7","0","",""
"9/6/2014","3170168","123652278","Smartphone","2329043291","Bing and Yahoo! search","22951990795","ballet class","Broad","0.00","0","1","1.00","0.00","0.00","0.00","0","0","",""
"9/6/2014","3170168","123652278","Smartphone","2329043291","Bing and Yahoo! search","22951990797","ballet 24"" classes","Broad","0.00","0","1","1.00","0.00","0.00","0.00","0","0","",""
"9/6/2014","3170168","123652278","Smartphone","2329043291","Bing and Yahoo! search","22951990797","ballet classes","Broad","0.00","0","1","1.00","0.00","0.00","0.00","0","0","",""
"9/6/2014","3170168","123652278","Computer","2329043291","Bing and Yahoo! search","22951990817","\","Broad","0.00","0","1","1.00","0.00","0.00","0.00","5","0","",""
"9/6/2014","3170168","123652278","Computer","2329043293","Bing and Yahoo! search","22951990850","zumba classes","Broad","0.00","0","1","7.00","0.00","0.00","0.00","5","0","",""
"9/6/2014","3170168","123652278","Smartphone","2329043293","Bing and Yahoo! search","22951990850","zumba classes","Broad","0.00","0","4","1.00","0.00","0.00","0.00","5","0","",""
"9/6/2014","3170168","123652278","Computer","2329043293","Bing and Yahoo! search","22951990874","zumba lessons","Broad","0.00","0","1","2.00","0.00","0.00","0.00","0","0","",""

我的问题是我无法为 CSVReader 构造函数指定 " 转义字符
(即使其与引号字符相同)。
如果我这样做,CSVReader 就会变得疯狂,它将整个 CSV 行作为单个 CSV 单元格读取。

有没有其他人遇到过这个错误以及如何解决它?!

最佳答案

如果您使用 CsvReader 的默认设置,它将工作。

检查他们有这个 Unresolved 错误:sourceforge.net/p/opencsv/bugs/83 :

Actually, it works fine, just not the way you think. Its defaults are comma for separator, quote for the quote character, and backslash for the escape character. However, it understands two consecutive quote characters as an escaped quote character. So, if you just go with the defaults, it will work fine.

默认情况下,它可以用双引号转义双引号,但您的“真实”转义字符必须仍然是其他字符。

所以下面的工作:

CSVReader reader = new CSVReader(new FileReader(App.class.getClassLoader().getResource("csv.csv").getFile()), ',','"','-');
  • 逗号作为分隔符
  • 双引号作为引号字符
  • 破折号(任何其他字符)作为转义字符

起初我把'\'作为转义字符,但是后来,你的字段“\”需要修改以转义转义字符。

关于java - CSVReader - 使用“for escape char 时的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com