[newbie] Reading a CSV file

  • Hi all,

    I'm trying to figure out how to open a text file and parse it using Cocoa
    (not unix calls).

    Which classes should I be concentrating on?

    Thanks

    Carl Gherardi
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • look at NSString in particular + (id)stringWithContentsOfFile:(NSString
    *)path and - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag as
    well as the class NSScanner.

    On Wednesday, May 29, 2002, at 05:22  PM, Carl Gherardi wrote:

    > Hi all,
    >
    > I'm trying to figure out how to open a text file and parse it using
    > Cocoa
    > (not unix calls).
    >
    > Which classes should I be concentrating on?
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Here's some code that I am working on, to appear in a future
    MacTech article.  Feel free to make use of it (and comment on
    it)!

    @interface NSString ( tsv )

    - (NSArray *) arrayFromTSV;

    @end

    @implementation NSString ( tsv )

    /*"    Build an array of dictionaries from a TSV (tab-separated
    values) string.  Blank lines and lines starting with "#" are
    ignored.  The first non-empty, non-comment line is assumed to be
    the keys.  Empty lines are not added to the array, so blank
    lines (or lines with just tabs) are safely ignored.  Missing
    items aren't added to the dictionary.  The string is assumed to
    be separated with UNIX newlines only.
    "*/

    - (NSArray *) arrayFromTSV
    {
    NSMutableArray *result = [NSMutableArray array];
    NSArray  *lines  = [self 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:@"\t"];
        keyCount = [keys count];
      }
      else // A data line
      {
        NSMutableDictionary *lineDict = [NSMutableDictionary dictionary];
        NSArray    *values  = [theLine componentsSeparatedByString:@"\t"];
        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]];
        }
        }
        if ([lineDict count]) // only add the line if there was any data
        {
        [result addObject:lineDict];
        }
      }
      }
    }
    return result;
    }

    @end

    On Wednesday, May 29, 2002, at 12:52  am, Carl Gherardi wrote:

    > Hi all,
    >
    > I'm trying to figure out how to open a text file and parse it
    > using Cocoa
    > (not unix calls).
    >
    > Which classes should I be concentrating on?
    >
    > Thanks
    >
    > Carl Gherardi
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    >
    >

    --
    Dan Wood
    Karelia Software, LLC
    <dwood...>
    http://www.karelia.com/
    Watson for Mac OS X: http://www.karelia.com/watson/
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Sunday, June 2, 2002, at 11:18  PM, Dan Wood wrote:

    > Here's some code that I am working on, to appear in a future MacTech
    > article.  Feel free to make use of it (and comment on it)!
    >
    > NSArray        *lines        = [self componentsSeparatedByString:@"\n"];

    To separate a text file by lines, use NSString's
    getLineStart:end:contentsEnd:range instead. It's only a little more
    work, but it will properly handle various line endings for you (UNIX,
    Mac, DOS, etc.).

    Regards.

    ..................................................................
    Itrat Khan
    Modeless Software, Inc.
    http://www.modeless.com
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.