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

后端 未结 3 1665
情话喂你
情话喂你 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条回答
  •  臣服心动
    2021-01-15 15:27

    In the CsvPreference.EXCEL_PREFERENCE you've given, the quote character is the " as described in the javadoc. The quote character is a character you use to wrap special characters that want you want to appear literally.

    As such, for these preferences, the appropriate way to produce your CSV content would be

    id, name, city, age
    1,"""Bob""",London,12
    

    Otherwise, the CSV parser simply thinks

    "Bob"
    

    means, literally,

    Bob
    

    since there is no other special character between the quotes. But a quote is a special character so if it appears between quotes, it will be considered, literally, as a quote.

    Alternatively, provide a different CsvPreference object which has a different quote character.

    Make this decision only after you are certain about what your CSV producer is sending you.

提交回复
热议问题