CSV parser in JAVA, double quotes in string (SuperCSV, OpenCSV)

后端 未结 3 1662
情话喂你
情话喂你 2021-01-15 14:31

all day I\'ve been searching how to resolve this probem and nothing... I want to write function, which convert CSV file to collection of lists (of strings). Here is this fun

3条回答
  •  梦毁少年i
    2021-01-15 15:13

    It's not clear from your question whether you're asking....

    1. My data contains quotes - why are they being stripped out?

    In this case, I'd point you to the CSV specification as your CSV file is not properly escaped, so those quotes aren't actually part of your data.

    It should be

    1,""Bob"",London,12

    not

    1,"Bob",London,12

    2. How do I apply quotes when writing (even if the data doesn't contain commas, quotes, etc)?

    By default Super CSV only escapes if necessary (the field contains a comma, double quote or newline).

    If you really want to enable quotes, then you can configure Super CSV with a quote mode.

    For example, you could always quote the name column in your example with the following preferences:

    private static final CsvPreference ALWAYS_QUOTE_NAME_COL = 
        new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE)
        .useQuoteMode(new ColumnQuoteMode(2)).build();
    

    Alternatively, if you want to quote everything then you can use AlwaysQuoteMode, or if you want a completely custom solution, then you can write your own QuoteMode.

提交回复
热议问题