CSV Parsing

28 views
Skip to first unread message

Northwest Mobile Development LLC

unread,
Apr 10, 2012, 12:16:45 AM4/10/12
to pdx-cocoa...@googlegroups.com
Hello All:

I am trying to parse a CSV file, and am having difficulty.  Can someone tell me where I am going wrong?  I am trying to read a csv file to an array of dictionaries that I plan to read into a tableview.  Here is my code:

NSString    *thePath = [[NSBundle mainBundle] pathForResource:@"AllAthleteBios" ofType:@"csv"];

   

    NSArray *allathletebios = [thePath componentsSeparatedByCharactersInSet:
                         [NSCharacterSet characterSetWithCharactersInString;@","]];
    

    

    NSDictionary    *aDictionary = [NSDictionary dictionaryWithContentsOfFile:thePath]; 

    

    

    

    self.athletes = [aDictionary valueForKey:@"AllAthleteBios"];

    

    displayitems = [[NSMutableArray alloc] initWithArray:athletes];
----------------
William Frowine, PMP
Northwest Mobile Development LLC
in...@nwmobiledev.com


Janine Ohmer

unread,
Apr 10, 2012, 12:35:15 AM4/10/12
to pdx-cocoa...@googlegroups.com
Hmm... I'm not exactly sure what this is supposed to do, but it doesn't look to me like it would work.  Have you seen this article?  It gives you some code to do this:


If you're still having trouble after trying their approach, seeing a few lines of the CSV file and some info about the incorrect result you're getting will help us sort it out.

janine

William Frowine, PMP

unread,
Apr 10, 2012, 1:08:03 AM4/10/12
to pdx-cocoa...@googlegroups.com
Thanks Janine:

So I modified the code to the following and made the following changes:

1. AbsoluteURL to Allathletebios
2. Type csv
3. occurances of float to string
It appears I have errors all over the place.  

- (BOOL)readFromURL:(NSURL *)Allathletebios ofType:(NSString *)csv 
error:(NSError **)outError 
    {
        NSString *fileString = [NSString stringWithContentsOfURL:@"Allathletes" 
                                                        encoding:NSUTF8StringEncoding error:outError];
        if ( nil == @"Allathletebios" ) return NO;
        NSScanner *scanner = [NSScanner scannerWithString:@"Allathletebios"];
        [scanner setCharactersToBeSkipped:
         [NSCharacterSet characterSetWithCharactersInString:@"\n, "]];
        NSMutableArray *newAthletes = [NSMutableArray array];
        string name, team;
        while ( [scanner scanString:&name] && [scanner scanString:&team] ) {
            [newAthletes addObject:
             [NSMutableDictionary dictionaryWithObjectsAndKeys:
              [NSString string:name], @"name",
              [NSString string:team], @"team",
              nil]];
        }
        [self setAthletes:newAthletes];
        return YES

Doug Mason

unread,
Apr 10, 2012, 1:21:31 AM4/10/12
to pdx-cocoa...@googlegroups.com
What errors are you getting, and could we get some sample data from your CSV file so that we can see if we can reproduce the error?

Thanks,
Doug

William Frowine, PMP

unread,
Apr 10, 2012, 1:31:30 AM4/10/12
to pdx-cocoa...@googlegroups.com
Here is the csv file:

AllAthleteBios.csv
PastedGraphic-1.png

Janine Ohmer

unread,
Apr 10, 2012, 1:33:08 AM4/10/12
to pdx-cocoa...@googlegroups.com
Yes, we can't really help you without seeing the data.

Also, this doesn't look right:

if ( nil == @"Allathletebios" ) return NO;

I'm guessing you want to check fileString for nil, not the NSString. I'm also not sure what

        string name, team;

is - I'm not aware of there being a class or type called "string".

I'm heading for bed, so will look at this tomorrow if you can provide more info (and no-one else solves it first).

janine

William Frowine, PMP

unread,
Apr 10, 2012, 1:45:33 AM4/10/12
to pdx-cocoa...@googlegroups.com
Janine:

I was assuming those were fields from the example's file.  I'm also going to bed.  Thanks for the help.  

Doug Mason

unread,
Apr 10, 2012, 10:08:14 AM4/10/12
to pdx-cocoa...@googlegroups.com
I have put this together pretty quickly, try this as your function and see if that helps you out Bill.

- (BOOL)readFromURL:(NSURL *)Allathletebios ofType:(NSString *)csv 

              error:(NSError **)outError 

{

    if( nil == Allathletebios ){ return NO;}

    NSString *fileString = [NSString stringWithContentsOfURL:Allathletebios 

                                                    encoding:NSUTF8StringEncoding error:outError];

    NSScanner *scanner = [NSScanner scannerWithString:fileString];

    [scanner setCharactersToBeSkipped:

     [NSCharacterSet characterSetWithCharactersInString:@"\n, "]];

    NSMutableArray *newAthletes = [NSMutableArray array];

    NSString *name, *team;

    while ( [scanner scanUpToCharactersFromSet:[scanner charactersToBeSkipped] intoString:&name] && [scanner scanUpToCharactersFromSet:[scanner charactersToBeSkipped] intoString:&team] ) {

        [newAthletes addObject:

         [NSMutableDictionary dictionaryWithObjectsAndKeys:

          [NSString stringWithString:name], @"name",

          [NSString stringWithString:team], @"team",

          nil]];

    }

    [self setAthletes:newAthletes];

    return YES

}



Thanks,

Doug
From: "William Frowine, PMP" <wfro...@gmail.com>
Reply-To: <pdx-cocoa...@googlegroups.com>
Date: Mon, 9 Apr 2012 22:45:33 -0700
To: <pdx-cocoa...@googlegroups.com>
Subject: Re: [pdx-cocoa-talk] CSV Parsing

Northwest Mobile Development LLC

unread,
Apr 10, 2012, 11:11:05 AM4/10/12
to pdx-cocoa...@googlegroups.com
Hi Doug:

Thanks so I am one step closer.  

I am getting the following errors:

1. Use of undeclared identifier "readfromURL
Here is the line : readFromURL:(NSURL *)Allathletebios ofType:(NSString *)csv 
error:(NSError **)outError 

2. Expected ;
         [NSCharacterSet characterSetWithCharactersInString:@"\n, "]];
if you add the ; where it wants it, you get an expected expression so it looks like this:
[scanner setCharactersToBeSkipped:
         [NSCharacterSet characterSetWithCharactersInString:@"\n, ";]];

        

3. Undeclared identifier for "scanner"

scanner scanUpToCharactersFromSet:[scanner charactersToBeSkipped] intoString:&team] )


So I am assuming two things:
A. The file that I am reading is in the application bundle's main directory.
B. the resulting array is called new athletes.  So here is the code now.

- (BOOL)readFromURL:(NSURL *)Allathletebios ofType:(NSString *)csv 
error:(NSError **)outError 
    {
        if( nil == Allathletebios ){ return NO;}
        NSString *fileString = [NSString stringWithContentsOfURL:Allathletebios 
                                                        encoding:NSUTF8StringEncoding error:outError];
        NSScanner *scanner = [NSScanner scannerWithString:fileString];
        [scanner setCharactersToBeSkipped:
         [NSCharacterSet characterSetWithCharactersInString:@"\n, ";]];
        NSMutableArray *newAthletes = [NSMutableArray array];
        NSString *name, *team;
        while ( [scanner scanUpToCharactersFromSet:[scanner charactersToBeSkipped] intoString:&name] && [scanner scanUpToCharactersFromSet:[scanner charactersToBeSkipped] intoString:&team] ) {
            [newAthletes addObject:
             [NSMutableDictionary dictionaryWithObjectsAndKeys:
              [NSString stringWithString:name], @"name",
              [NSString stringWithString:team], @"team",
              nil]];
        }
        [self setAthletes:newAthletes];

      }

----------------
William Frowine, PMP
Northwest Mobile Development LLC
in...@nwmobiledev.com


Reply all
Reply to author
Forward
0 new messages