ios 在哪里可以找到用于 Objective-C 的 CSV 到 NSArray 解析器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3344628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where can I find a CSV to NSArray parser for Objective-C?
提问by Toby Allen
I'm looking for an easy to use CSV parser for Objective-C to use on the iPhone. Where can I find one?
我正在寻找一个易于使用的 CSV 解析器,以便在 iPhone 上使用 Objective-C。我在哪里可以找到一个?
I'm also looking for other parsers such as JSON, so maybe there is a conversion library somewhere.
我也在寻找其他解析器,例如 JSON,所以也许某处有一个转换库。
回答by Dave DeLong
I finally got around to cleaning up a parser I've had in my code folder and posted it on Github: http://github.com/davedelong/CHCSVParser
我终于开始清理代码文件夹中的解析器并将其发布在 Github 上:http: //github.com/davedelong/CHCSVParser
It's quite thorough. It handles all sorts of escaping schemes, newlines in fields, comments, etc. It also uses intelligent file loading, which means you can safely parse huge files in constrained memory conditions.
说的很透彻。它处理各种转义方案、字段中的换行符、注释等。它还使用智能文件加载,这意味着您可以在有限的内存条件下安全地解析大文件。
回答by gose
Here's a simple category on NSString to parse a CSV string that has commas embedded inside quote blocks.
这里有一个关于NSString的简单分类解析有嵌入的引号块内部逗号一个CSV字符串。
#import "NSString+CSV.h"
@implementation NSString (CSV)
- (NSArray *)componentsSeparatedByComma
{
BOOL insideQuote = NO;
NSMutableArray *results = [[NSMutableArray alloc] init];
NSMutableArray *tmp = [[NSMutableArray alloc] init];
for (NSString *s in [self componentsSeparatedByString:@","]) {
if ([s rangeOfString:@"\""].location == NSNotFound) {
if (insideQuote) {
[tmp addObject:s];
} else {
[results addObject:s];
}
} else {
if (insideQuote) {
insideQuote = NO;
[tmp addObject:s];
[results addObject:[tmp componentsJoinedByString:@","]];
tmp = nil;
tmp = [[NSMutableArray alloc] init];
} else {
insideQuote = YES;
[tmp addObject:s];
}
}
}
return results;
}
@end
This assumes you've read your CSV file into an array already:
这假设您已经将 CSV 文件读入数组:
myArray = [myData componentsSeparatedByString:@"\n"];
The code doesn't account for escaped quotes, but it could easily be extended to.
该代码没有考虑转义引号,但它可以很容易地扩展到。
回答by xmr
Quick way to do this:
快速执行此操作的方法:
NSString *dataStr = [NSString stringWithContentsOfFile:@"example.csv" encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [dataStr componentsSeparatedByString: @","];
NSString *dataStr = [NSString stringWithContentsOfFile:@"example.csv" encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [dataStr componentsSeparatedByString: @","];
回答by JeanNicolas
Well, above simple solutions doesn't take into account multiple records. Use the following code reading a default excel CSV using ASCI 13 as line end marker:
好吧,上面的简单解决方案没有考虑多条记录。使用以下代码读取使用 ASCI 13 作为行结束标记的默认 excel CSV:
NSString *content = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
NSArray *contentArray = [content componentsSeparatedByString:@"\r"]; // CSV ends with ACSI 13 CR (if stored on a Mac Excel 2008)
for (NSString *item in contentArray) {
NSArray *itemArray = [item componentsSeparatedByString:@";"];
// log first item
NSLog(@"%@",[itemArray objectAtIndex:0]);
}
回答by mipadi
I wrote a dead-simple (although not fully-featured) CSV parser for a project I was working on: CSVFile.h
and CSVFile.m
. Feel free to grab it -- the code is available under the GPLv3 (unfortunately, it was a requirement for the project I was working on) but I'd be happy to license it to you under an MIT license or another license.
我为我正在从事的项目编写了一个非常简单(虽然功能不全)的 CSV 解析器:CSVFile.h
和CSVFile.m
. 随意获取它——代码在 GPLv3 下可用(不幸的是,这是我正在从事的项目的要求),但我很乐意根据 MIT 许可证或其他许可证将其许可给您。
回答by Toby Allen
This seems to be the most comprehensive that I've found so far.
这似乎是迄今为止我找到的最全面的。
http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data
http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data
As a side note, you'd think most major languages (Delphi, C#, Objective-c, php etc) would have a library available with a full implementation of this basic data interchange format.
作为旁注,您会认为大多数主要语言(Delphi、C#、Objective-c、php 等)都会有一个库,其中包含这种基本数据交换格式的完整实现。
I know json is cool and XML is reliable but neither are available as a save option from most applications saving table data. CSV still is.
我知道 json 很酷,而 XML 很可靠,但大多数应用程序都不能作为保存表数据的保存选项。CSV 仍然是。
回答by OgreSwamp
回答by Barnaby Byrne
As xmr said above: It's possible in Objective C to convert an NSString csv into 'components separated by string' array.
正如 xmr 上面所说:在 Objective C 中可以将 NSString csv 转换为“由字符串分隔的组件”数组。
NSArray* items;
items=[bufferString componentsSeparatedByString:@","];
In case you are interested in csv export having arrived at this thread - as I did - here is an extract of how I exported a csv file.
如果您对到达此线程的 csv 导出感兴趣 - 正如我所做的那样 - 这里是我如何导出 csv 文件的摘录。
NSString* fileName = @"Level";
fileName = [fileName stringByAppendingString:levelNumberBeingEdited];
fileName = [fileName stringByAppendingString:@".txt"];
NSString* bufferString=@"";
Buffer String is populated by looping through each data item (not shown) and inserting a comma between each. Finally it's exported.
缓冲区字符串是通过循环遍历每个数据项(未显示)并在每个数据项之间插入一个逗号来填充的。终于可以导出了。
NSString* homeDir = NSHomeDirectory();
NSString* fullPath = [homeDir stringByAppendingPathComponent:fileName];
NSError* error = nil;
[bufferString writeToFile:fullPath atomically:NO encoding:NSASCIIStringEncoding error:&error];