【问题标题】:Univocity Parser: TextParsingException while parsing a line which has a starting double quote(") but does not have an ending double quote(")Univocity Parser:TextParsingException,同时解析具有起始双引号(“)但没有结束双引号(”)的行
【发布时间】:2017-02-07 23:03:15
【问题描述】:

解析文件时出现异常:

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

【问题讨论】:

  • @JeronimoBackes:你能看看这个吗?
  • 为什么你的第二行有双引号?我想这就是问题
  • 是的。这似乎是问题的原因。但我无法控制文件内容。
  • 希望..有什么办法可以解决这个问题吗?按原样解析内容直到分隔符?
  • 我对这个 API 不是很熟悉,虽然你可以寻找一些可以在解析时忽略引号的方法,否则你将不得不扩展这个类并实现你自己的逻辑

标签: java csv parsing univocity


【解决方案1】:

这就是 CSV 解析器的工作方式。如果找到引用,那是因为引用之后的内容可以包含分隔符、行尾或其他(希望是)转义的引号。

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

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

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

【讨论】:

    猜你喜欢
    • 2018-09-05
    • 1970-01-01
    • 2013-07-02
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多