开发者

NSScanner and a CSV file

开发者 https://www.devze.com 2023-02-18 20:42 出处:网络
I have a CSV file with four fields, \"Woonplaats\", \"Gemeente\", \"Provincie\", \"Latitude\", and \"Longitude\".

I have a CSV file with four fields, "Woonplaats", "Gemeente", "Provincie", "Latitude", and "Longitude".

Example values:

Graveland,Wijdemeren,Noord-Holland,52.24412000,5.12150000

Using the code below, I get the string in my text, and then I want to save it in array. How should I 开发者_如何转开发use NSScanner to get data from this string and save in an array containing dictionaries?

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@"csv"];
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ];
NSScanner *scanner = [NSScanner scannerWithString:myText];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n ,"]];
NSMutableArray *newPoints = [NSMutableArray array];           


I believe this is what you're looking for. I used Dan Wood forum post and modified it for your need.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...

    NSString *path =@"cities.csv";
    NSError *error;
    NSString *stringFromFileAtPath = [[NSString alloc]
                                      initWithContentsOfFile:path
                                      encoding:NSUTF8StringEncoding
                                      error:&error];

    NSMutableDictionary *lineDict = [NSMutableDictionary dictionary];
    NSArray  *lines  = [stringFromFileAtPath componentsSeparatedByString:@"\n"];
    NSEnumerator*theEnum = [lines objectEnumerator];
    NSArray  *keys  = nil;
    int  keyCount = 0;
    NSString *theLine;

    while (nil != (theLine = [theEnum nextObject]) )
    {
        if (![theLine isEqualToString:@""] && ![theLine hasPrefix:@"#"])    // ignore empty lines and lines that start with #
        {
            if (nil == keys) // Is keys not set yet? If so, process first real line as list of keys
            {
                keys = [theLine componentsSeparatedByString:@","];
                keyCount = [keys count];
            }
            else // A data line
            {
                NSArray    *values  = [theLine componentsSeparatedByString:@","];
                int valueCount = [values count];
                int i;

                for ( i = 0 ; i < keyCount && i < valueCount ; i++ )
                {
                    NSString *value = [values objectAtIndex:i];
                    if (nil != value && ![value isEqualToString:@""])
                    {
                        [lineDict setObject:value forKey:[keys objectAtIndex:i]];
                    }
                }
            }
        }
    }

    for (id key in lineDict)
    {
        NSLog(@"key: %@, value: %@", key, [lineDict objectForKey:key]);
    }

    [pool drain];
    return 0;
}

And the output is:

2011-07-13 20:02:41.898 cities[5964:903] key: Latitude, value: 52.24412000
2011-07-13 20:02:41.900 cities[5964:903] key: Provincie, value: Noord-Holland
2011-07-13 20:02:41.900 cities[5964:903] key: Longitude, value: 5.12150000
2011-07-13 20:02:41.901 cities[5964:903] key: Gemeente, value: Wijdemeren
2011-07-13 20:02:41.902 cities[5964:903] key: Woonplaats, value: Graveland
0

精彩评论

暂无评论...
验证码 换一张
取 消