CSV reader creating problem with special character (“),("),('),(/) while reading the CSV file by java code?

huangapple go评论74阅读模式
英文:

CSV reader creating problem with special character (“),("),('),(/) while reading the CSV file by java code?

问题

示例数据:

```csv
"Here Comes Santa Claus (来自“完美音调2”原声带)","PITCH PERFECT 2",NULL,"Here Comes ameda Claus (来自“阿门完美2”原声带)",NULL,"HERE COMES SANTA CLAUS (来自“完美音调2”原声带)",NULL,"PITCH IMPACT 2","00-00-0000 00:00:00".

使用以下的 pom.xml 依赖来读取文件:

<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>4.1</version>
</dependency>

使用以下的 Java 代码:

RFC4180Parser rfc4180Parser = new RFC4180ParserBuilder().build();
CSVReaderBuilder csvReaderBuilder = new CSVReaderBuilder(filereader)
                    .withCSVParser(rfc4180Parser);
csvReader = csvReaderBuilder.build();
String[] nextRecord;
int i=0;
            while ((nextRecord = csvReader.readNext()) != null) {
    System.out.println("Data " + nextRecord[i]);
    System.out.println("New Records");

}

我的输出:

Here Comes Santa Claus (来自“完美音调2”原声带)","PITCH PERFECT 2",NULL,"Here Comes ameda Claus (来自“阿门完美2”原声带)",NULL,"HERE COMES SANTA CLAUS (来自“完美音调2”原声带)
New Records
NULL
New Records
PITCH IMPACT 2
New Records
00-00-0000 00:00:00
New Records

输出应该像这样:--

Comes Comes Santa Amen(来自“Amen Amen2” Amen)
New Records
Amen2Amen22
New Records
NULL
New Records
Amen2 ameda Claus (来自“阿门 Amen2 2” Amen)
New Records
NULL
New Records
HERE COMES SANTA CLAUS (来自“完美音调2”原声带)
New Records
NULL
New Records
PITCH IMPACT 2
New Records
00-00-0000 00:00:00
New Records
英文:

Sample Data:

&quot;Here Comes Santa Claus (from “pitch Perfect 2&quot; Soundtrack)&quot;,&quot;PITCH PERFECT 2&quot;,NULL,&quot;Here Comes ameda Claus (from “amen Perfect 2&quot; Amen)&quot;,NULL,&quot;HERE COMES SANTA CLAUS (FROM “PITCH PERFECT 2&quot; SOUNDTRACK)&quot;,NULL,&quot;PITCH IMPACT 2&quot;,&quot;00-00-0000 00:00:00&quot;.

Use this pom.xml dependency to read file :

&lt;dependency&gt;
		&lt;groupId&gt;com.opencsv&lt;/groupId&gt;
		&lt;artifactId&gt;opencsv&lt;/artifactId&gt;
		&lt;version&gt;4.1&lt;/version&gt;
&lt;/dependency&gt;

Using the below java code :

RFC4180Parser rfc4180Parser = new RFC4180ParserBuilder().build();
CSVReaderBuilder csvReaderBuilder = new CSVReaderBuilder(filereader)
					.withCSVParser(rfc4180Parser);
csvReader = csvReaderBuilder.build();
String[] nextRecord;
int i=0;
			while ((nextRecord = csvReader.readNext()) != null) {
    System.out.println(&quot;Data &quot; + nextRecord [i]);
    System.out.println(&quot;New Records&quot;);

}

My output :-

Here Comes Santa Claus (from “pitch Perfect 2&quot; Soundtrack)&quot;,&quot;PITCH PERFECT 2&quot;,NULL,&quot;Here Comes ameda Claus (from “amen Perfect 2&quot; Amen)&quot;,NULL,&quot;HERE COMES SANTA CLAUS (FROM “PITCH PERFECT 2&quot; SOUNDTRACK)
New Records
NULL
New Records
PITCH IMPACT 2
New Records
00-00-0000 00:00:00
New Records

Output should be like this :--

Comes Comes Santa Amen(from “Amen Amen2&quot; Amen)
New Records
Amen2Amen22
New Records
NULL
New Records
Amen2 ameda Claus (from “amen Amen2 2&quot; Amen)
New Records
NULL
New Records
HERE COMES SANTA CLAUS (FROM “PITCH PERFECT 2&quot; SOUNDTRACK)
New Records
NULL
New Records
PITCH IMPACT 2
New Records
00-00-0000 00:00:00
New Records

答案1

得分: 1

如@Janoz所说,你的CSV文件无效,因为在数值中有需要转义的引号(&quot;)。

由于你正在使用RFC 4180解析器,你需要通过重复引号来进行转义:

&quot;Here Comes Santa Claus(来自“完美音调2”原声带)&quot;,&quot;PITCH PERFECT 2&quot;,NULL,&quot;Here Comes ameda Claus(来自“完美音调2的“阿门”)&quot;,NULL,&quot;HERE COMES SANTA CLAUS(来自“完美音调2”原声带)&quot;,NULL,&quot;PITCH IMPACT 2&quot;,&quot;00-00-0000 00:00:00&quot;。
英文:

As @Janoz says, your CSV isn't valid, because you have quotes (&quot;) inside the values that need to be escaped.

Since you are using a RFC 4180 parser you need to escape them by duplicating the quotes:

&quot;Here Comes Santa Claus (from “pitch Perfect 2&quot;&quot; Soundtrack)&quot;,&quot;PITCH PERFECT 2&quot;,NULL,&quot;Here Comes ameda Claus (from “amen Perfect 2&quot;&quot; Amen)&quot;,NULL,&quot;HERE COMES SANTA CLAUS (FROM “PITCH PERFECT 2&quot;&quot; SOUNDTRACK)&quot;,NULL,&quot;PITCH IMPACT 2&quot;,&quot;00-00-0000 00:00:00&quot;.

huangapple
  • 本文由 发表于 2020年9月9日 15:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63806331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定