Search code examples
javaregexcsvopencsv

OpenCSV not escaping the quotes(")


I have a CSV file which will have delimiter or unclosed quotes inside a quotes, How do i make CSVReader ignore the quotes and delimiters inside quotes. For example:

123|Bhajji|Maga|39|"I said Hey|" I am "5|'10."|"I a do "you"|get that"

This is the content of file.

The below program to read the csv file.

@Test
public void readFromCsv() throws IOException {
    FileInputStream fis = new FileInputStream(
            "/home/netspurt/awesomefile.csv");
    InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    CSVReader reader = new CSVReader(isr, '|', '\"');

    for (String[] row; (row = reader.readNext()) != null;) {
        System.out.println(Arrays.toString(row));
    }
    reader.close();
    isr.close();
    fis.close();
}

I get the o/p something like this.

[123, Bhajji, Maga, 39, I said Hey| I am "5|'10., I am an idiot do "you|get that]

what happened to quote after you

Edit: The Opencsv dependency com.opencsv opencsv 3.4


Solution

  • As the CSV format specifies the quotes(") if its inside a field we need to precede it by another quote("). So this solved my problem.

    123|Bhajji|Maga|39|"I said Hey|"" I am ""5|'10."|"I a do ""you""|get that"
    

    Refrence: https://www.ietf.org/rfc/rfc4180.txt