0

I have csv that comes with format:

a1, a2, a3, "a4,a5", a6

Only field with , will have quotes

Using Objective-C, how to easily parse this? I try to avoid using open source CSV parser as company policy. Thanks.

stream
  • 369
  • 1
  • 7
  • 18
  • 1
    Field values with newlines will also be wrapped in quotes. Parsing all possible CSV formatting is much more difficult than most people realize. Use an open source library (make your life easy) or bite the bullet and write all of your own code to deal with all of the possibilities. There is no "easily parse this". – rmaddy Feb 27 '13 at 03:59
  • Why is your question an exact duplicate (except replace Objective-C for Java) of this one from 16 months ago: http://stackoverflow.com/questions/7800494/parse-csv-with-double-quote-in-some-cases?rq=1 – rmaddy Feb 27 '13 at 04:00
  • But I want to realize it in Objective-C. – stream Feb 27 '13 at 04:02
  • Fine, but why didn't you write your own question. It just seems weird to copy another question verbatim, including title, question, and tags. – rmaddy Feb 27 '13 at 04:03
  • My fault,my question is the same to that.I only want to get a solution quickly. – stream Feb 27 '13 at 04:06
  • Writing code to parse CSV is well beyond the scope of a SO question. Either use an open source library or base your own code on one that has a license that allows you to make a copy of it. – rmaddy Feb 27 '13 at 04:09
  • Why don't you use Mark Byers answer or similar to that? – Anoop Vaidya Feb 27 '13 at 04:15

2 Answers2

1

I agree with rmaddy that a full csv parsing algorithm is beyond the scope of SO, however, here is one possible way of tackling this:

  1. Extract the whole line into an NSString
  2. Iterate over the NSString, pushing each character back into another string.
  3. When a comma is encountered, save the stored string, ignore the comma and create a new blank string.
  4. Repeat until end of line.
  5. Set a flag to identify whether double quotes have been found. If the flag is set, ignore step 3. When a second set of quotes is found, unset the flag and continue as before.

This is generally applicable to any language (using their respective native string classes) and such an algorithm can form a small basis for a full CSV parser. In this particular case however, you may not need any more functionality than this.

For some sample code, I would encourage you to have a look at my answer to this CSV-related question as it demonstrates a way of splitting and storing strings in Objective-C.

Community
  • 1
  • 1
Ephemera
  • 8,672
  • 8
  • 44
  • 84
0

This snippet worked perfectly for me...

    BOOL quotesOn = false;
    NSString* line = @"a1, a2, a3, "a4,a5", a6";
    NSMutableArray* lineParts = [[NSMutableArray alloc] init];
    NSMutableString* linePart = [[NSMutableString alloc] init];
    for (int i = 0; i < line.length; i++)
    {
        unichar current = [line characterAtIndex: i];
        if (current == '"')
        {
            quotesOn = !quotesOn;
            continue;
        }
        if (!quotesOn && current == ',')
        {
            if (linePart.length > 0)
                [lineParts addObject: linePart];
            linePart = [[NSMutableString alloc] init];
        }
        if (quotesOn || current != ',')
            [linePart appendString: [line substringWithRange: NSMakeRange(i, 1)]];
    }
    if (linePart.length > 0)
        [lineParts addObject: linePart];

My 5 elements are in the lineParts array...

Aace
  • 1,792
  • 16
  • 15