Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Ignore commas in quoted elements while parsing CSV file

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You specify the configuration as constructor arguments when you create an instance of CSVParser or CSVReader respectively.
 
Ed Dablin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
reply
    Bookmark Topic Watch Topic
  • New Topic