0

I am parsing a csv using apache common csv but i am getting more records then it exists in csv that is due to in my csv i have some of the value containing record separator (\n) enclosed with double quotes.

https://commons.apache.org/proper/commons-csv/

Example:

test, "test
test 2",test 3, test 4 

I am expecting o/p as

Record 1: test
Record 2: 
test
test 2
Record 3: test 3
Record 4: test 4

But i am getting like this

Record 1: test
Record 2: test
Record 3: test 2
Record 4: test 3
Record 5: test 4

Here is the code which i am using for parsing currently.

CSVParser parser = CSVParser.parse(reader,CSVFormat.RFC4180.withFirstRecordAsHeader() .withQuote(null));

Just to add Earlier i was facing issue with having field delimiter in record field value which got resolved with .withQuote(null).

Any clue on how can we solve this?

  • Show your code. – kenny_k Jan 29 '18 at 12:30
  • We are always glad to help and support new coders but you need to help yourself first. :-) [After doing more research](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem post what you've tried with a clear explanation of what isn't working and provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Dave Jan 29 '18 at 12:32
  • Here is the code which i am using for parsing currently. CSVParser parser = CSVParser.parse(reader, CSVFormat.RFC4180.withFirstRecordAsHeader() .withQuote(null)); Just to add Earlier i was facing issue with having field delimiter in record field value which got resolved with .withQuote(null). – piyush gajjariya Jan 29 '18 at 17:07

1 Answers1

-1

I do not see how this can work when you set the quote character to null. RFC4180 sets the quote character to double quote which is what you want here.

Gary Gregory
  • 439
  • 5
  • 7
  • If i don't see withQuote(null) as below then i am getting this run time exception " (line 12494) invalid char between encapsulated token and delimiter". which looks this issue https://stackoverflow.com/questions/26729799/invalid-char-between-encapsulated-token-and-delimiter-in-apache-commons-csv-libr so i added withQuote(null) to fix that. CSVParser parser = CSVParser.parse(reader, CSVFormat.RFC4180.withFirstRecordAsHeader()); Any other work-around of this? – piyush gajjariya Jan 30 '18 at 02:38