Apache Commons CSV:带引号的输入不起作用 [英] Apache commons CSV: quoted input doesn't work
本文介绍了Apache Commons CSV:带引号的输入不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.io.IOException;
import java.io.StringReader;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
我尝试使用Apache CSV解析器解析一个简单的csv文件.只要我不使用引号,它就可以正常工作.当我尝试在输入中添加引号
I try to parse a simple csv file with Apache CSV parser. It works fine as long as I don't use quotes. When I try to add a quote to the input
"a";42
它给了我错误:
invalid char between encapsulated token and delimiter
这是一个简单,完整的代码:
Here is a simple, complete code:
public class Test {
public static void main(String[] args) throws IOException {
String DATA = "\"a\";12";
CSVParser csvParser =
CSVFormat.EXCEL
.withIgnoreEmptyLines()
.withIgnoreHeaderCase()
.withRecordSeparator('\n').withQuote('"')
.withEscape('\\').withRecordSeparator(';').withTrim()
.parse(new StringReader(DATA));
}
}
我根本无法找出代码中缺少的内容.
I simply can't find out what I've missed in the code.
推荐答案
问题是如此的微不足道,我错过了.
The problem was so trivial I missed it.
我使用withRecordSeparator
而不是withDelimiter
来设置字段分隔符.
I used withRecordSeparator
instead of withDelimiter
to set the field separator.
这按我预期的那样工作:
This works as I expected:
public class Test {
public static void main(String[] args) throws IOException {
String DATA = "\"a\";12";
CSVParser csvParser =
CSVFormat.EXCEL
.withIgnoreEmptyLines()
.withIgnoreHeaderCase()
.withRecordSeparator('\n').withQuote('"')
.withEscape('\\').withDelimeter(';').withTrim()
.parse(new StringReader(DATA));
}
}
这篇关于Apache Commons CSV:带引号的输入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文