parsing - CSV parser in JAVA, double quotes in string (SuperCSV, OpenCSV) -
all day i've been searching how resolve probem , nothing... want write function, convert csv file collection of lists (of strings). here function:
public collection<? extends list<string>> parse() throws ioexception { collection<list<string>> collectionoflists = new arraylist<list<string>>(); csvlistreader parser = new csvlistreader(files.newbufferedreader(pathtofile, standardcharsets.utf_8), csvpreference.excel_preference); list<string> row; while( (row = parser.read()) != null) collectionoflists.add(row); return collectionoflists; } public static string tostring(collection<? extends list<string>> csv) { stringbuilder builder = new stringbuilder(); for(list<string> l : csv) { for(string s : l) builder.append(s).append(','); if(builder.length() > 0) builder.setcharat(builder.length()-1,'\n'); } return builder.tostring(); }
but e.g. input:
id, name, city, age 1,"bob",london,12
output tostring(parse()) is:
id, name, city, age 1,bob,london,12
instead of same input:/ can do, strings contain \" (quotes) ? please me.
you create own preference.
csvpreference excelpreference = new csvpreference.builder('\'', ',', "\n").build(); csvlistreader parser = new csvlistreader(files.newbufferedreader(pathtofile , standardcharsets.utf_8), excelpreference);
after that, output expected. in example, strip single quote if have in csv file , keep double quote untouched.
Comments
Post a Comment