I have to parse a csv file which has fields that can look like the following:
("FOO, BAR BAZ", 42)
And yield the two fields:
FOO, BAR BAZ
42
I'm not sure how to do this succinctly using Apache Commons CSV or OpenCSV, so I'm looking for some guidance. It may just be that I don't fully understand the org.apache.commons.csv.CSVFormat
property "quoteChar" which is touched on in the documentation but never clearly explained anywhere I could find. If so, it'd be very helpful if you could point me towards better documentation of that feature.
Here's a brief example that shows my problem as well as what I've tried and the results:
String test = "(\"FOO, BAR BAZ\", 42)";
int numTries = 5;
CSVParser[] tries = new CSVParser[numTries];
tries[0] = CSVParser.parse(line, CSVFormat.DEFAULT.withRecordSeparator("\n"));//BAR BAZ"
tries[1] = CSVParser.parse(line, CSVFormat.DEFAULT.withQuote('"'));//BAR BAZ"
tries[2] = CSVParser.parse(line, CSVFormat.DEFAULT.withQuote(null));//BAR BAZ"
tries[3] = CSVParser.parse(line, CSVFormat.DEFAULT.withQuote('"').withQuoteMode(QuoteMode.NON_NUMERIC));//BAR BAZ"
tries[4] = CSVParser.parse(line, CSVFormat.DEFAULT.withRecordSeparator(")\n("));//BAR BAZ"
for(int i = 0; i < numTries; i++){
CSVRecord record = tries[i].getRecords().get(0);
System.out.println(record.get(1));//.equals("42"));
}
Note that it works fine if you exclude the parentheses from the input.