CSVReader - bug when using " for escape char

后端 未结 3 1843
小鲜肉
小鲜肉 2021-01-15 14:55

I am using OpenCSV.

I have a CSVReader trying to parse a CSV file.
That file has quote char \" and separator char , an

3条回答
  •  情话喂你
    2021-01-15 15:27

    The CSVReader is not fully RFC4180 compliant. Use their newer CSV reader (RFC4180Parser):

    RFC4180Parser rfc4180Parser = new RFC4180ParserBuilder().build();
    CSVReaderBuilder csvReaderBuilder = new CSVReaderBuilder(
        new FileReader("input.csv"));
    
    CSVReader reader = csvReaderBuilder
        .withCSVParser(rfc4180Parser)
        .build();
    

    To read a String line formatted as a CSV:

    String test = "ballet 24\"\" classes";
    String[] columns = new RFC4180Parser().parseLine(test);
    

    To use the reader (an alternative is reader.readNext()):

    for (String[] line : reader.readAll()) {
      for (String s : line) {
        System.out.println(s);
      }
    }
    

    See http://opencsv.sourceforge.net/#rfc4180parser for more details.

    Code taken from GeekPrompt

提交回复
热议问题