使用解析CSV数据时,我会遇到一个错误
java.lang.IllegalStateException: IOException reading next record: java.io.IOException:
(line 46196) invalid char between encapsulated token and delimiter
我使用的设置如下:
try {
File csvInput = getLatestFilefromDir(CSV_PATH);
reader = new FileReader(csvInput);
final CSVFormat csvFormat = CSVFormat.Builder.create()
.setHeader(HEADERS)
.setDelimiter(';')
.setQuote('"')
.setEscape('\\')
.setSkipHeaderRecord(true)
.build();
Iterable<CSVRecord> csvRecords = csvFormat.parse(reader);
for (CSVRecord csvRecord : csvRecords) {
// processing
}
} catch (Exception e) {
log.error("Error retrieving CSV data.");
e.printStackTrace();
}
由于错误提示数据有一些缺陷,无效条目:
"TABLE_NAME";"ATTRIBUTE";"VALUE"
"SWAP_LEG_TYPE";"SWAP_LEG_TYPE_DESC";"The payments (PAY or RECEIVE) of this \"Leg\" are based on the yield linked to a specific equity or an index. (or to the actual market price of the equity or the index ???)"
"CNTPTY_TYPE";"CNTPTY_TYPE_DESC";"With Local Government we mean the so called \Regional Governments or Local Authorities\\" (RGLA) as defined by the EBA (European Banking Authority).\""
改变数据是我无法控制的。假设反斜杠用于转义引号,就像在其他示例中一样,在本例中使用不当,并将其转换为CSV文件,希望有
...Authorities\ \" (RGLA)...
在解析之前是否有一种替换字符串的方法?或者我能做些什么来扩展CSVFormat
构建器来接受这些数据?
我正在考虑一种简单的方法来读取整个输入,只需为\
执行替换字符串\
,因为这是百万行中唯一的实例,但这似乎是错误的。
发布于 2022-09-21 02:44:28
这是一个稍微修改的原始版本,应该解决您的问题,setQuote(null)
做了所有的魔术。
final CSVFormat csvFormat = CSVFormat.Builder.create()
.setHeader(HEADERS)
.setDelimiter(';')
.setQuote(null)
.setEscape('\\')
.setSkipHeaderRecord(true)
.build();
https://stackoverflow.com/questions/73778318
复制相似问题