目标c:读取csv文件

发布于 2024-10-28 22:04:17 字数 194 浏览 1 评论 0原文

我有这种类型的文件:

@firstTablel: 1#one#two#third#four; 2#苹果#塔#花#机器人;

这是我的 csv 文件的示例...“firstTable”是我的表,1 和 2 是我的两个项目的两个 id。

我如何在 Objective C 中读取这个文件?你能帮助我吗?

I have this type of file:

@firstTablel: 1#one#two#three#four;
2#apple#tower#flower#robot;

this is an example of my csv file..."firstTable" is my table and 1 and 2 are two id of my two item.

How I can read this file in objective c? Can you help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦萦几度 2024-11-04 22:04:17

Cocoa for Scientifics 有一篇关于读取 CSV 文件的精彩文章:http://macresearch .org/cocoa-scientists-part-xxvi-parsing-csv-data

在您的情况下,您需要执行以下操作:

NSString *fileString = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:outError];
if (!fileString) {
    NSLog(@"Error reading file.");
}
NSScanner *scanner = [NSScanner scannerWithString:fileString];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n#; "]];

NSString *id = nil, *hashOne = nil, *hashTwo = nil, *hashThree = nil, *hashFour = nil;
while ([scanner scanUpToString:@"#" intoString:&id] && [scanner scanUpToString:@"#" intoString:&hashOne] && [scanner scanUpToString:@"#" intoString:&hashTwo] && [scanner scanUpToString:@"#" intoString:&hashThree] && [scanner scanUpToString:@";" intoString:&hashFour]) {
     // Process the values as needed.
     NSLog(@"id:%@, 1:%@, 2:%@, 3:%@:, 4:%@", id, hashOne, hashTwo, hashThree, hashFour);
}

在您提供的示例中,这将打印:

2011-03-31 13:55:07.846 Stephen[39304:903] id:1, 1:one, 2:two, 3:three:, 4:four
2011-03-31 13:55:07.849 Stephen[39304:903] id:2, 1:apple, 2:tower, 3:flower:, 4:robot

Cocoa for Scientists has a great post on reading CSV files: http://macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data

In your case, you'd do something like:

NSString *fileString = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:outError];
if (!fileString) {
    NSLog(@"Error reading file.");
}
NSScanner *scanner = [NSScanner scannerWithString:fileString];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n#; "]];

NSString *id = nil, *hashOne = nil, *hashTwo = nil, *hashThree = nil, *hashFour = nil;
while ([scanner scanUpToString:@"#" intoString:&id] && [scanner scanUpToString:@"#" intoString:&hashOne] && [scanner scanUpToString:@"#" intoString:&hashTwo] && [scanner scanUpToString:@"#" intoString:&hashThree] && [scanner scanUpToString:@";" intoString:&hashFour]) {
     // Process the values as needed.
     NSLog(@"id:%@, 1:%@, 2:%@, 3:%@:, 4:%@", id, hashOne, hashTwo, hashThree, hashFour);
}

On the sample you've provided, this will print:

2011-03-31 13:55:07.846 Stephen[39304:903] id:1, 1:one, 2:two, 3:three:, 4:four
2011-03-31 13:55:07.849 Stephen[39304:903] id:2, 1:apple, 2:tower, 3:flower:, 4:robot
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文