gpt4 book ai didi

iphone - 从 csv 文件加载数据 (iphone SDK)

转载 作者:行者123 更新时间:2023-12-03 21:12:13 26 4
gpt4 key购买 nike

有人知道如何从 csv 文件加载数据吗?

对于代码示例,CPTestAppScatterPlotController.m我从 core-plot Google 网站下载的,可以根据随机绘制折线图生成初始数据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中有两列数据,一列为时间,一列为数据)有人可以给我一些建议吗???谢谢!!!!

最佳答案

http://cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html

这里是创建 CSV 解析器的好起点。它包含示例代码和用户评论。

关于iphone - 从 csv 文件加载数据 (iphone SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2426374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com