Using NScanner to parse CSV File to Dictionary Array [duplicate]

帅比萌擦擦* 提交于 2019-12-25 19:03:47

问题


I've created an iPhone app that has a dictionary array of locations (lat,long,point). I created the array by manually entering each value.

myLocationArray = @[
                 @{
                   kStation : @"1",
                   kLatitude : @( 41.656467),
                   kLongitude : @(-81.277963)
                   },
                 @{
                   kStation : @"2",
                   kLatitude : @(41.657118),
                   kLongitude : @(-81.276545)
                   },
                 @{
                   kStation : @"3",
                   kLatitude : @(41.658493),
                   kLongitude : @(-81.273542)
                   },
                  ...

This is good and works but now I want to create this array programmatically by getting the data from a .CSV file. I have a .CSV file (TestCSV.csv) that looks like this.

41.656467,-81.277963,27200
41.657118,-81.276545,27650
41.658493,-81.273542,28631.5
41.660728,-81.268547,30195
41.661830,-81.266065,30991
41.662828,-81.263819,31700
41.663677,-81.261962,32300
41.664578,-81.259909,32950
41.666210,-81.256312,34100
41.666921,-81.254708,34605
41.668043,-81.252191,35400
41.669044,-81.250043,36099

I'd like to create myLocationArray (with formatting as shown) by parsing TestCSV.csv using NScanner. I've set up to parse the my data file.

NSString *pathToFile =[[NSBundle mainBundle] pathForResource:@"TestCSV" ofType: @"csv"];

NSString *fileString = [NSString stringWithContentsOfFile:pathToFile     encoding:NSUTF8StringEncoding error:nil];

if (!fileString) {
NSLog(@"Error reading file.");
}

NSScanner *scanner = [NSScanner scannerWithString:fileString];

I need help from here though. I've looked at many examples but it seems like this is where I need some code custom to my application. Thanks in advance for your time.


回答1:


I don't understand why you're bothering with a scanner at all, seeing as your structure is so simple and predictable. Start with an empty NSMutableArray. You can split the file into lines like this:

NSArray *lines = [input componentsSeparatedByString:@"\n"];

Now enumerate that array. For each line, you can split the line at the commas:

NSArray *nums = [oneLine componentsSeparatedByString:@","];

The array nums is an array of three NSString values. For the first two items of the array, convert them to doubles with doubleValue and wrap that in an NSNumber. Make the NSDictionary for that line and add it to the NSMutableArray.




回答2:


Don't try to re-invent the wheel, as there are a couple of pitfalls in the CSV file format (which is not even especially well defined).

There are for instance the special cases of newlines and commas within a field. Any algorithm based on just splitting by lines and commas is going produce incorrect results given such input. Also notice that Excel doesn't always use a comma as a separator, depending on the locale.

Use a library like CHCSVParser, which will cover those pitfalls.



来源:https://stackoverflow.com/questions/16503084/using-nscanner-to-parse-csv-file-to-dictionary-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!