aspose file tools*
The moose likes Beginning Java and the fly likes Ignore commas in quoted elements while parsing CSV file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Ignore commas in quoted elements while parsing CSV file" Watch "Ignore commas in quoted elements while parsing CSV file" New topic
Author

Ignore commas in quoted elements while parsing CSV file

Ed Dablin
Ranch Hand

Joined: Oct 09, 2012
Posts: 32
I'm working with a large CSV data file.
Some of the data elements contain commas. In these cases the element is enclosed by quotation marks.
For example:
, , , blah , blah blah , Murder Incorporated Inc. , "Murder Incorporated, Inc." , blah blah blah , , ,
I want my parser to ignore the comma between the quotes.
I've learned that OpenCSV supports "ignoring commas in quoted elements".
OpenCSV
But I can't find the method to do so.
Help appreciated....
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1773
    
    7

You specify the configuration as constructor arguments when you create an instance of CSVParser or CSVReader respectively.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Ed Dablin
Ranch Hand

Joined: Oct 09, 2012
Posts: 32
Jelle Klap wrote:You specify the configuration as constructor arguments when you create an instance of CSVParser or CSVReader respectively.

Sorry, I don't know what you mean
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1773
    
    7

If you look at the API documentation I linked to for either of the two classes, you'll find several overloaded constructors for each class.
Those overloaded constructors allow you to configure their behavior, for instance (also in the OpenCSV FAQ.):


etc.

Unless you instantiate a CSVParser with the overloaded constructor that accepts the ignoreQuotations boolean flag, the default behaviour of the CSVParser should be to ignore seperator characters within quoted sections, unless quotes are escaped with the default/specified escapeChar, which means your sample data should be parsed the way you want it to. That is unless you set the strictQuotes flag, which will cause any data between seperators that is not quoted to be returned as an empty String value.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Ed Dablin wrote:Sorry, I don't know what you mean...

I hate to sound brutal, but if you still don't after reading the documentation and the links that Jelle provided, then maybe you're not quite ready for this problem.
Programming is not simple, and it's no failure to admit that you can't do something. In fact, it's a sign of wisdom - and not just in programming.

Just remember - all it means is you can't do it YET (←click).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
 
subject: Ignore commas in quoted elements while parsing CSV file