Parsing CSV: how can NSScanner recognize empty field (i.e. ,,)?
Posted
by
Fabrizio Prosperi
on Stack Overflow
See other posts from Stack Overflow
or by Fabrizio Prosperi
Published on 2011-04-22T14:25:17Z
Indexed on
2012/07/08
3:16 UTC
Read the original article
Hit count: 192
I am very new to Xcode and trying - as millions - to parse a CSV file. I have read many contributions and I am managing it but I have a problem when my NSScanner intercepts an empty field: "Field_A, Field_B,, Field_D". I guess it is because it ignores empty space by default, or in this case no space at all.
String is:
"Personal","2011-01-01","Personal","Cigarettes",,4.60,"Cash","",
I tried to debug it using scanLocation:
2011-04-22 15:57:32.414 Spending[42015:a0f] Before while...scan location is:0
2011-04-22 15:57:32.414 Spending[42015:a0f] Account: "Personal" - scan location is:10
2011-04-22 15:57:32.415 Spending[42015:a0f] Date: "2011-01-01" - scan location is:23
2011-04-22 15:57:32.415 Spending[42015:a0f] Category: "Personal" - scan location is:34
2011-04-22 15:57:32.416 Spending[42015:a0f] Subcategory: "Cigarettes" - scan location is:47
2011-04-22 15:57:32.416 Spending[42015:a0f] Income: 4.600000 - scan location is:53
2011-04-22 15:57:32.416 Spending[42015:a0f] Expense: 0.000000 - scan location is:53
2011-04-22 15:57:32.417 Spending[42015:a0f] Payment: "Cash" - scan location is:60
2011-04-22 15:57:32.417 Spending[42015:a0f] Note: "" - scan location is:63
And as you can see after that even expense field gets no value (should be 4.60).
Here is the relevant piece of code:
NSScanner *scanner = [NSScanner scannerWithString:fileString];
[scanner setCharactersToBeSkipped: [NSCharacterSet characterSetWithCharactersInString:@"\n, "]];
NSString *account, *date, *category, *subcategory, *payment, *note;
float income, expense;
// Set up data delimiter using comma
NSCharacterSet *commaSet;
commaSet = [NSCharacterSet characterSetWithCharactersInString:@","];
NSLog (@"Before while...scan location is:%d\n", scanner.scanLocation);
[scanner scanUpToCharactersFromSet:commaSet intoString:&account];
NSLog(@"Account: %@ - scan location is:%d\n",account, scanner.scanLocation);
[scanner scanUpToCharactersFromSet:commaSet intoString:&date];
NSLog(@"Date: %@ - scan location is:%d\n",date, scanner.scanLocation);
[scanner scanUpToCharactersFromSet:commaSet intoString:&category];
NSLog(@"Category: %@ - scan location is:%d\n",category, scanner.scanLocation);
[scanner scanUpToCharactersFromSet:commaSet intoString:&subcategory];
NSLog(@"Subcategory: %@ - scan location is:%d\n",subcategory, scanner.scanLocation);
[scanner scanFloat:&income];
NSLog(@"Income: %f - scan location is:%d\n",income, scanner.scanLocation);
[scanner scanFloat:&expense];
NSLog(@"Expense: %f - scan location is:%d\n",expense, scanner.scanLocation);
[scanner scanUpToCharactersFromSet:commaSet intoString:&payment];
NSLog(@"Payment: %@ - scan location is:%d\n",payment, scanner.scanLocation);
[scanner scanUpToCharactersFromSet:commaSet intoString:¬e];
NSLog(@"Note: %@\n - scan location is:%d",note, scanner.scanLocation);
I tried looking carefully through NSScanner Class Reference, but could not get an idea? Do you have any?
Thanks, Fabrizio.
© Stack Overflow or respective owner