在使用Apache Commons CSV库解析CSV文件时,我得到了以下错误。
Exception in thread "main" java.io.IOException: (line 2) invalid char between encapsulated token and delimiter
at org.apache.commons.csv.Lexer.parseEncapsulatedToken(Lexer.java:275)
at org.apache.commons.csv.Lexer.nextToken(Lexer.java:152)
at org.apache.commons.csv.CSVParser.nextRecord(CSVParser.java:450)
at org.apache.commons.csv.CSVParser.getRecords(CSVParser.java:327)
at parse.csv.file.CSVFileParser.main(CSVFileParser.java:29)
这个错误是什么意思?
发布于 2016-03-31 17:55:11
当我们在数据中嵌入引用时,我们遇到了这个问题。
0,"020"1,"BS:5252525 ORDER:99999"4
应用的解决方案是CSVFormat csvFileFormat = CSVFormat.DEFAULT.withQuote(null);
@Cuga提示帮助我们解决了问题。谢谢@Cuga
完整的代码是
public static void main(String[] args) throws IOException {
FileReader fileReader = null;
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withQuote(null);
String fileName = "test.csv";
fileReader = new FileReader(fileName);
CSVParser csvFileParser = new CSVParser(fileReader, csvFileFormat);
List<CSVRecord> csvRecords = csvFileParser.getRecords();
for (CSVRecord csvRecord : csvRecords) {
System.out.println(csvRecord);
}
csvFileParser.close();
}
结果是
CSVRecord [comment=null, mapping=null, recordNumber=1, values=[0, "020"1, "BS:5252525 ORDER:99999"4]]
发布于 2014-11-04 15:52:31
CSV文件中的该行包含一个单元格与行尾、文件末尾或下一个单元格之间的无效字符。一个非常常见的原因是无法转义封装字符(用于“包装”每个单元格的字符,因此CSV知道单元格(令牌)的开始和结束位置。
发布于 2014-11-04 20:05:08
我找到了这个问题的解决方案。我的其中一个CSV文件具有如下属性:带有嵌套"quote“的”“属性"
由于属性中嵌套的引号,解析器失败。
要避免上述问题,请按如下方式对嵌套引号进行转义:"attribute with nested“引号"
这是解决问题的一种方法。
https://stackoverflow.com/questions/26729799
复制相似问题