Invalid char between encapsulated token and delimiter in Apache Commons CSV library

后端 未结 5 1645
有刺的猬
有刺的猬 2021-01-03 20:13

I am getting the following error while parsing the CSV file using the Apache Commons CSV library.

Exception in thread \"main\" java.io.IOException: (line 2)          


        
5条回答
  •  孤城傲影
    2021-01-03 20:30

    We ran into this issue when we had embedded quote in our data.

    0,"020"1,"BS:5252525  ORDER:99999"4
    

    Solution applied was CSVFormat csvFileFormat = CSVFormat.DEFAULT.withQuote(null);

    @Cuga tip helped us to resolve. Thanks @Cuga

    Full code is

        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 csvRecords = csvFileParser.getRecords();
    
        for (CSVRecord csvRecord : csvRecords) {
            System.out.println(csvRecord);
        }
        csvFileParser.close();
    }
    

    Result is

    CSVRecord [comment=null, mapping=null, recordNumber=1, values=[0, "020"1, "BS:5252525  ORDER:99999"4]]
    

提交回复
热议问题