I'm looking for an easy to use CSV parser for Objective-C to use on the iPhone. Where can I find one?
I'm also looking for other parsers such as JSON, so maybe there is a conversion library somewhere.
I'm looking for an easy to use CSV parser for Objective-C to use on the iPhone. Where can I find one?
I'm also looking for other parsers such as JSON, so maybe there is a conversion library somewhere.
I finally got around to cleaning up a parser I've had in my code folder and posted it on Github: http://github.com/davedelong/CHCSVParser
It's quite thorough. It handles all sorts of escaping schemes, newlines in fields, comments, etc. It also uses intelligent file loading, which means you can safely parse huge files in constrained memory conditions.
This seems to be the most comprehensive that I've found so far.
http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data
As a side note, you'd think most major languages (Delphi, C#, Objective-c, php etc) would have a library available with a full implementation of this basic data interchange format.
I know json is cool and XML is reliable but neither are available as a save option from most applications saving table data. CSV still is.
Here's a simple category on NSString to parse a CSV string that has commas embedded inside quote blocks.
This assumes you've read your CSV file into an array already:
The code doesn't account for escaped quotes, but it could easily be extended to.
As xmr said above: It's possible in Objective C to convert an NSString csv into 'components separated by string' array.
In case you are interested in csv export having arrived at this thread - as I did - here is an extract of how I exported a csv file.
Buffer String is populated by looping through each data item (not shown) and inserting a comma between each. Finally it's exported.
Well, above simple solutions doesn't take into account multiple records. Use the following code reading a default excel CSV using ASCI 13 as line end marker:
Quick way to do this:
NSString *dataStr = [NSString stringWithContentsOfFile:@"example.csv" encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [dataStr componentsSeparatedByString: @","];