java - Univocity 解析器 : TextParsingException while parsing a line which has a starting double quote (") but does not have an ending double quote(")

标签 java csv parsing univocity

解析文件时出现异常:

com.univocity.parsers.common.TextParsingException: Length of parsed input (4097) exceeds the maximum number of characters defined in your parser settings (4096). 
Identified line separator characters in the parsed content. This may be the cause of the error. The line separator in your parser settings is set to '\r\n'. Parsed content: The quick brown fox jumps over the lazy dog.|[\n]

文件内容:

1234|5678|The quick brown fox jumps over the lazy dog.|
1234|5678|"The quick brown fox jumps over the lazy dog.|
1234|5678|The quick brown fox jumps over the lazy dog.|
1234|5678|The quick brown fox jumps over the lazy dog.|
1234|5678|The quick brown fox jumps over the lazy dog.|
.........
.........
1234|5678|The quick brown fox jumps over the lazy dog.|

我正在使用以下 CSV 解析器设置:

CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setLineSeparatorDetectionEnabled(true);
parserSettings.getFormat().setDelimiter('|');
parserSettings.setIgnoreLeadingWhitespaces(true);
parserSettings.setIgnoreTrailingWhitespaces(true);
parserSettings.setHeaderExtractionEnabled(false);
parserSettings.setMaxCharsPerColumn(4096);

我可以从异常中推断出,在第二行中我有一个起始双引号 (")。但该行不以双引号 (") 结尾。 所以在这种情况下,列长度达到 EOF(文件末尾)。

测试构建:2.2.2

最佳答案

这就是 CSV 解析器的工作原理。如果找到引号,那是因为引号后面的内容可以包含定界符、行结尾或其他(希望如此)转义引号。

在您的案例中,解决这种情况的唯一方法是执行以下操作:

parserSettings.getFormat().setQuote('\0');

这将使解析器忽略引号和处理值,并将它们作为未引号的值。找到行结束符或分隔符后,将按您的预期收集该值。

关于java - Univocity 解析器 : TextParsingException while parsing a line which has a starting double quote (") but does not have an ending double quote("),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773370/

相关文章:

java - Elasticsearch 响应时间慢

java - CXF 中的 Web 服务错误

c# - Entity Framework Core - 如何可编程性关闭自动增量?

excel - 使用管道分隔符将 Excel 导出为 CSV,无需更改列表分隔符设置

string - 解析命令行参数来处理 bash 中的文件列表?

parsing - JSON-LD帧: Forcing duplication of redundant properties values

Java 代码 Cipher.doFinal(byte[]) 的 Javascript/NodeJS 等效代码?

java - 为什么tomcat看不到某些文件夹?

mysql - 将 CSV 数据导入到单独的 MYSQL 表中

json - Grails 2.5.6 如何解析请求 JSON 并将其映射到 POGO?