Iphone iOS中的CSV行解析

Iphone iOS中的CSV行解析,iphone,objective-c,parsing,csv,Iphone,Objective C,Parsing,Csv,我正在分析objective-c中的CSV文件。该文件包含如下内容: line 40 | Rising searches line 41 | nabi avcı,Breakout line 42 | stonewall,+700% line 43 | medgar evers,+500% line 44 | lgbt,+350% line 45 | roe v wade,+350% line 46 | αÏεÏγια,+250% 我想得到第41行到第50行(含)的内容。然后我想把每

我正在分析objective-c中的CSV文件。该文件包含如下内容:

line 40 | Rising searches
line 41 | nabi avcı,Breakout
line 42 | stonewall,+700%
line 43 | medgar evers,+500%
line 44 | lgbt,+350%
line 45 | roe v wade,+350%
line 46 | αÏεÏγια,+250%
我想得到第41行到第50行(含)的内容。然后我想把每一行分成两个
NSStrings
,一个包含
前面的内容,
,另一个包含后面的内容。我该怎么做


非常感谢您的帮助。谢谢!安托万

试着和戴夫·德隆的CHCSVParser玩一玩

您可以使用CSV文件的路径初始化解析器(假设您有一个CHCSVParser*\u parser实例变量):

然后,您应该结合使用三种委托方法来定制解析器并使其适合您的需要:

- (void)parser:(CHCSVParser *)parser didBeginLine:(NSUInteger)recordNumber
{
    // Only parse the fields on lines 41 to 50
    // _shouldParseLine is an ivar that is set to YES
    // only when the fields inside the following line or lines
    // should be parsed.
    if (recordNumber == 41) {
        _shouldParseLine = YES;
    }
}

- (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber 
{
    if (recordNumber == 50) {
        // The parser has finished parsing the 50th line
        // We're done, cancel any further parsing.
        // It is not necessary to set _shouldParseLine to NO, 
        // since the parser is killed here and the didReadField
        // delegate method will not be called again.
        [parser cancelParsing];
    }
}

- (void)parser:(CHCSVParser *)parser didReadField:(NSString *)field atIndex:(NSInteger)fieldIndex
{
    if (_shouldParseLine == YES) {
        // Here are your fields.
        // The field at index 0 consists of the text
        // before the comma, the field at index 1
        // consists of the text after the comma.
    }
}

我会使用JSON。更具可读性,易于解析,获取您需要的任何数据,无论范围如何…@SavaMazăre,OP可能不会生成要解析的数据。还有很多CSV格式的数据,特别是因为从Excel导出非常容易。
- (void)parser:(CHCSVParser *)parser didBeginLine:(NSUInteger)recordNumber
{
    // Only parse the fields on lines 41 to 50
    // _shouldParseLine is an ivar that is set to YES
    // only when the fields inside the following line or lines
    // should be parsed.
    if (recordNumber == 41) {
        _shouldParseLine = YES;
    }
}

- (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber 
{
    if (recordNumber == 50) {
        // The parser has finished parsing the 50th line
        // We're done, cancel any further parsing.
        // It is not necessary to set _shouldParseLine to NO, 
        // since the parser is killed here and the didReadField
        // delegate method will not be called again.
        [parser cancelParsing];
    }
}

- (void)parser:(CHCSVParser *)parser didReadField:(NSString *)field atIndex:(NSInteger)fieldIndex
{
    if (_shouldParseLine == YES) {
        // Here are your fields.
        // The field at index 0 consists of the text
        // before the comma, the field at index 1
        // consists of the text after the comma.
    }
}