英文:
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:
"Here Comes Santa Claus (from “pitch Perfect 2" Soundtrack)","PITCH PERFECT 2",NULL,"Here Comes ameda Claus (from “amen Perfect 2" Amen)",NULL,"HERE COMES SANTA CLAUS (FROM “PITCH PERFECT 2" SOUNDTRACK)",NULL,"PITCH IMPACT 2","00-00-0000 00:00:00".
Use this pom.xml dependency to read file :
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.1</version>
</dependency>
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("Data " + nextRecord [i]);
System.out.println("New Records");
}
My output :-
Here Comes Santa Claus (from “pitch Perfect 2" Soundtrack)","PITCH PERFECT 2",NULL,"Here Comes ameda Claus (from “amen Perfect 2" Amen)",NULL,"HERE COMES SANTA CLAUS (FROM “PITCH PERFECT 2" 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" Amen)
New Records
Amen2Amen22
New Records
NULL
New Records
Amen2 ameda Claus (from “amen Amen2 2" Amen)
New Records
NULL
New Records
HERE COMES SANTA CLAUS (FROM “PITCH PERFECT 2" SOUNDTRACK)
New Records
NULL
New Records
PITCH IMPACT 2
New Records
00-00-0000 00:00:00
New Records
答案1
得分: 1
如@Janoz所说,你的CSV文件无效,因为在数值中有需要转义的引号("
)。
由于你正在使用RFC 4180解析器,你需要通过重复引号来进行转义:
"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"。
英文:
As @Janoz says, your CSV isn't valid, because you have quotes ("
) 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:
"Here Comes Santa Claus (from “pitch Perfect 2"" Soundtrack)","PITCH PERFECT 2",NULL,"Here Comes ameda Claus (from “amen Perfect 2"" Amen)",NULL,"HERE COMES SANTA CLAUS (FROM “PITCH PERFECT 2"" SOUNDTRACK)",NULL,"PITCH IMPACT 2","00-00-0000 00:00:00".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论