CSVReader-使用“;逃逸字符 [英] CSVReader - bug when using " for escape char

查看:134
本文介绍了CSVReader-使用“;逃逸字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCSV.

I am using OpenCSV.

我有一个 CSVReader 试图解析CSV文件.
该文件的引号是char "和分隔符char ,转义字符也是" .

I have a CSVReader trying to parse a CSV file.
That file has quote char " and separator char , and escape char also ".

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

Note that the CSV contains cells like:

"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 构造函数指定转义字符的"(即使其与报价char相同).
如果这样做, CSVReader 就会发疯,它将整个CSV行作为单个CSV单元读取.

My problem is that I cannot specify " for escape char to the CSVReader constructor
(i.e. make it the same as the quote char).
If I do so, the CSVReader simply goes crazy, and it reads the whole CSV line as a single CSV cell.

还有其他人遇到过此错误以及如何解决该错误吗?!

Has anyone else encountered this bug and how to get around it?!

推荐答案

如果使用CsvReader的默认设置,它将起作用.

It will work if you go with the default settings for CsvReader.

检查该漏洞是否具有: sourceforge.net/p/opencsv/bugs/83 :

Check this open bug they have: 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.

默认情况下,它可以使用双引号对双引号进行转义,但是您的"true"转义字符仍必须是其他字符.

By default, it is able to escape double quote with double quote, but your 'true' escape character must still be something else.

因此,以下工作有效:

CSVReader reader = new CSVReader(new FileReader(App.class.getClassLoader().getResource("csv.csv").getFile()), ',','"','-');

  • 逗号作为分隔符
  • 双引号作为报价字符
  • 破折号(任何其他字符)作为转义字符
  • 首先,我将'\'用作转义字符,但是随后,您需要修改字段"\"以转义转义字符.

    At first I put '\' as escape character, but then, your field "\" would need to be modified to escape the escape character.

    这篇关于CSVReader-使用“;逃逸字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆