从csv文件加载数据(iphone SDk) [英] Load data from csv file (iphone SDk)
问题描述
有谁知道如何从csv文件加载数据?
Does anyone know how to load data from csv file?
对于代码示例,
CPTestAppScatterPlotController.m
我从core下载-lot Google网站,可以根据随机
生成初始数据x,y绘制折线图。
For the code example, CPTestAppScatterPlotController.m that I downloaded from core-plot Google website, a line graph can be plotted based on randomly generated initial data x, y.
// Add some initial data
SMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
NSUInteger i;
for ( i = 0; i < 60; i++ ) {
id x = [NSNumber numberWithFloat:1+i*0.05];
id y = [NSNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2];
[contentArray addObject:[NSMutableDictionary
dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}
self.dataForPlot = contentArray;
然后我修改了代码,
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ECG_Data" ofType:@"csv"];
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ];
NSScanner *scanner = [NSScanner scannerWithString:myText];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n, "]];
NSMutableArray *newPoints = [NSMutableArray array];
float time, data;
while ( [scanner scanFloat:&time] && [scanner scanFloat:&data] ) {
[newPoints addObject:
[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:time], @"time",
[NSNumber numberWithFloat:data], @"data",
nil]];
}
self.dataForPlot = newPoints;
似乎我的代码无法从csv文件读取数据。
(ECG_Data.csv中有两个数据列,一个用于时间,一个用于数据)
任何人都可以给我一些建议吗?
感谢!!!!
It seems that my code could not read the data from csv file. (there are two cols of the data in ECG_Data.csv, one for time and one for data) Can anyone give me some suggestion??? Thanks!!!!
推荐答案
http://cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html
这是一个创建CSV解析器的好地方。它包含示例代码和用户评论。
Here is a good place to start for creating a CSV parser. It's complete with sample code and user comments.
这篇关于从csv文件加载数据(iphone SDk)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!