在 xCode 中读取“CSV”文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10364271/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:07:44  来源:igfitidea点击:

Reading 'CSV' file in xCode

iphoneobjective-ciosxcodecsv

提问by shebi

Could you please help me for reading this csv file?

你能帮我阅读这个 csv 文件吗?

CSV Output

CSV 输出

"^GSPC",1403.36,"4/27/2012","4:32pm",+3.38,1400.19,1406.64,1397.31,574422720 "^IXIC",3069.20,"4/27/2012","5:30pm",+18.59,3060.34,3076.44,3043.30,0

"^GSPC",1403.36,"4/27/2012","4:32pm",+3.38,1400.19,1406.64,1397.31,574422720 "^IXIC",3069.20,"4/27/25:32pm" ",+18.59,3060.34,3076.44,3043.30,0

ViewController.m

视图控制器.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *absoluteURL = @"http://myserver/data.csv";
    NSURL *url = [NSURL URLWithString:absoluteURL];
    NSString *fileString = [[NSString alloc] initWithContentsOfURL:url];
    NSScanner *scanner = [NSScanner scannerWithString:fileString];
    [scanner setCharactersToBeSkipped:
    [NSCharacterSet characterSetWithCharactersInString:@"\n, "]];
    NSMutableArray *newPoints = [NSMutableArray array];

}

采纳答案by dasblinkenlight

Handling CSV parsing is a lot tricker than your initial code implies, because quoted separators need to be ignored. For example,

处理 CSV 解析比初始代码暗示的要复杂得多,因为需要忽略带引号的分隔符。例如,

1,"Hello, world!",2

has three columns, but NSScannerwould find four tokens (1, "Hello, "world!", and 2).

有三列,但NSScanner会找到四个标记(1"Hello、“世界!”和2)。

There are many special cases to handle in order to build a full-featured CSV parser, so the fastest way to get it working is to use a pre-built one. For example, you could use the parser described in this answer on Stack Overflow.

为了构建功能齐全的 CSV 解析器,需要处理许多特殊情况,因此使其工作的最快方法是使用预构建的解析器。例如,您可以在 Stack Overflow 上使用此答案中描述的解析器。

回答by objectiveCoder

I might be too late to answer this but may be this answer can help someone to read CSV file with ease. This is what i Have don't to read CSV file

我可能来不及回答这个问题,但可能这个答案可以帮助某人轻松阅读 CSV 文件。这是我没有读过的 CSV 文件

   - (void)viewDidLoad {
    [super viewDidLoad];
   //  printing the file path

      NSMutableArray *colA = [NSMutableArray array];
      NSMutableArray *colB = [NSMutableArray array];
   NSMutableArray *colC = [NSMutableArray array];
   NSMutableArray *colD = [NSMutableArray array];
  //  NSString* fileContents = [NSTemporaryDirectory() 
stringByAppendingPathComponent:@"yourfile.csv"];
NSString *fileContents = [[NSBundle mainBundle] 
 pathForResource:@"yourfile" ofType:@"csv"];

  NSURL*URl = [NSURL fileURLWithPath:fileContents];

  //        NSString* fileContents = [NSString 
stringWithContentsOfURL:@"YourFile.csv"];
   NSError *error;
  NSString* fileContentss = (NSString *)[NSString 
 stringWithContentsOfURL:URl encoding:NSASCIIStringEncoding 
error:&error];

   NSMutableArray *newArra = [[NSMutableArray alloc] init];
   [fileContentss stringByReplacingOccurrencesOfString:@"\"" 
  withString:@""];
      NSArray* rows = [fileContentss 
   componentsSeparatedByString:@"\n"];

   NSMutableArray *roearr = [[NSMutableArray alloc] init];
   roearr = [rows mutableCopy];


    [roearr removeObjectAtIndex:[rows count] - 1];
      for (NSString *row in roearr){



          NSArray* columns = [row componentsSeparatedByString:@","];

          newArra = [columns mutableCopy];

         [newArra removeObjectAtIndex:[newArra count] - 1];
            [colA addObject:newArra[0]];
            [colB addObject:newArra[1]];
            [colC addObject:newArra[2]];
            [colD addObject:columns[3]];
       }

     NSLog(@"columns[0] %@",colA);
    NSLog(@"columns[0] %@",colB);
    NSLog(@"columns[0] %@",colC);
    NSLog(@"columns[0] %@",colD);


   // Do any additional setup after loading the view, typically from 
  a nib.
  }