CSVReader - bug when using " for escape char

后端 未结 3 1841
小鲜肉
小鲜肉 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条回答
  •  梦毁少年i
    2021-01-15 15:39

    It will work if you go with the default settings for CsvReader.

    Check this open bug they have: sourceforge.net/p/opencsv/bugs/83:

    Actually, it works fine, just not the way you think. Its defaults are comma for separator, quote for the quote character, and backslash for the escape character. However, it understands two consecutive quote characters as an escaped quote character. So, if you just go with the defaults, it will work fine.

    By default, it is able to escape double quote with double quote, but your 'true' escape character must still be something else.

    So the following works:

    CSVReader reader = new CSVReader(new FileReader(App.class.getClassLoader().getResource("csv.csv").getFile()), ',','"','-');
    
    • comma as separator
    • double quote as quote char
    • dash (any other character) as escape character

    At first I put '\' as escape character, but then, your field "\" would need to be modified to escape the escape character.

提交回复
热议问题