CSVReader - bug when using " for escape char

前端 未结 3 1730
忘掉有多难
忘掉有多难 2021-01-15 09:30

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 10:00

    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.

提交回复
热议问题