java - OpenCSV not escaping the quotes(") -
i have csv file have delimiter or unclosed quotes inside quotes, how make csvreader ignore quotes , delimiters inside quotes. example:
123|bhajji|maga|39|"i said hey|" "5|'10."|"i "you"|get that"
this content of file.
the below program read 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, '|', '\"'); (string[] row; (row = reader.readnext()) != null;) { system.out.println(arrays.tostring(row)); } reader.close(); isr.close(); fis.close(); }
i o/p this.
[123, bhajji, maga, 39, said hey| "5|'10., idiot "you|get that]
what happened quote after you
edit: opencsv dependency com.opencsv opencsv 3.4
from source code of com.opencsv:opencsv:
/** * constructs csvreader. * * @param reader reader underlying csv source. * @param separator delimiter use separating entries * @param quotechar character use quoted elements * @param escape character use escaping separator or quote */ public csvreader(reader reader, char separator, char quotechar, char escape) { this(reader, separator, quotechar, escape, default_skip_lines, csvparser.default_strict_quotes); }
see http://sourceforge.net/p/opencsv/source/ci/master/tree/src/main/java/com/opencsv/csvreader.java
there constructor additional parameter escape allows escape separators , quotes (as per javadoc).
Comments
Post a Comment