这里的问题是引号没有正确转义。您的解析器无法处理。尝试使用univocity-parsers,因为这是我所知道的唯一可以处理引用值内的非转义引号的java解析器。它也比 Commons CSV 快 4 倍。试试这个代码:
//configure the parser to handle your situation
CsvParserSettings settings = new CsvParserSettings();
settings.setHeaderExtractionEnabled(true); //uses first line as headers
settings.setUnescapedQuoteHandling(STOP_AT_CLOSING_QUOTE);
settings.trimQuotedValues(true); //trim whitespace around values in quotes
//create the parser
CsvParser parser = new CsvParser(settings);
String input = "" +
"Field1,Field2,Field3,Field4,Field5\n" +
"\"Ryan, R\"u\"bianes\",\" dummy@gmail.com\",\"29445\",\"626\",\"South delhi, Rohini 122001\"";
//parse your input
List<String[]> rows = parser.parseAll(new StringReader(input));
//print the parsed values
for(String[] row : rows){
for(String value : row){
System.out.println('[' + value + ']');
}
System.out.println("-----");
}
这将打印:
[Ryan, R"u"bianes]
[dummy@gmail.com]
[29445]
[626]
[South delhi, Rohini 122001]
-----
希望能帮助到你。
披露:我是这个库的作者,它是开源和免费的(Apache 2.0 许可证)