使用 NScanner 将 CSV 文件解析为字典数组 [英] Using NScanner to parse CSV File to Dictionary Array

查看:63
本文介绍了使用 NScanner 将 CSV 文件解析为字典数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 iPhone 应用程序,其中包含位置(纬度、经度、点)的字典数组.我通过手动输入每个值来创建数组.

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)
                   },
                  ...

这很好并且有效,但现在我想通过从 .CSV 文件中获取数据以编程方式创建这个数组.我有一个看起来像这样的 .CSV 文件 (TestCSV.csv).

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

我想通过使用 NScanner 解析 TestCSV.csv 来创建 myLocationArray(格式如图所示).我已经设置好解析我的数据文件.

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.

推荐答案

不要试图重新发明轮子,因为 CSV 文件格式(甚至没有特别明确定义)存在一些缺陷.

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).

例如,字段中有换行符和逗号的特殊情况.给定这样的输入,任何基于仅按行和逗号分割的算法都会产生不正确的结果.另请注意,根据区域设置,Excel 并不总是使用逗号作为分隔符.

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.

使用像 CHCSVParser 之类的库,它将涵盖这些陷阱.

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

这篇关于使用 NScanner 将 CSV 文件解析为字典数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆