Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find a CSV to NSArray parser for Objective-C? [closed]

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.

like image 600
Toby Allen Avatar asked Jul 27 '10 14:07

Toby Allen


4 Answers

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.

like image 72
Dave DeLong Avatar answered Nov 06 '22 17:11

Dave DeLong


Here's a simple category on NSString to parse a CSV string that has commas embedded inside quote blocks.

#import "NSString+CSV.h"

@implementation NSString (CSV)

- (NSArray *)componentsSeparatedByComma
{
    BOOL insideQuote = NO;
    NSMutableArray *results = [[NSMutableArray alloc] init];
    NSMutableArray *tmp = [[NSMutableArray alloc] init];

    for (NSString *s in [self componentsSeparatedByString:@","]) {
        if ([s rangeOfString:@"\""].location == NSNotFound) {
            if (insideQuote) {
                [tmp addObject:s];
            } else {
                [results addObject:s];
            }
        } else {
            if (insideQuote) {
                insideQuote = NO;
                [tmp addObject:s];
                [results addObject:[tmp componentsJoinedByString:@","]];
                tmp = nil;
                tmp = [[NSMutableArray alloc] init];
            } else {
                insideQuote = YES;
                [tmp addObject:s];
            }
        }
    }

    return results;
}

@end

This assumes you've read your CSV file into an array already:

myArray = [myData componentsSeparatedByString:@"\n"];

The code doesn't account for escaped quotes, but it could easily be extended to.

like image 36
gose Avatar answered Nov 06 '22 16:11

gose


Quick way to do this:

NSString *dataStr = [NSString stringWithContentsOfFile:@"example.csv" encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [dataStr componentsSeparatedByString: @","];

like image 8
xmr Avatar answered Nov 06 '22 16:11

xmr


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:

NSString *content =  [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
NSArray *contentArray = [content componentsSeparatedByString:@"\r"]; // CSV ends with ACSI 13 CR (if stored on a Mac Excel 2008)

for (NSString *item in contentArray) {
    NSArray *itemArray = [item componentsSeparatedByString:@";"];
    // log first item
    NSLog(@"%@",[itemArray objectAtIndex:0]);
}
like image 5
JeanNicolas Avatar answered Nov 06 '22 16:11

JeanNicolas