Iphone 阅读';CSV';xCode中的文件
你能帮我阅读这个csv文件吗 CSV输出 “^GSPC”,1403.36,“4/27/2012”,“下午4:32”,“3.381400.191406.641397.31574422720” “^IXIC”,3069.20,“2012年4月27日”,“下午5:30”,“+18.593060.343076.443043.30,0 ViewController.mIphone 阅读';CSV';xCode中的文件,iphone,objective-c,ios,xcode,csv,Iphone,Objective C,Ios,Xcode,Csv,你能帮我阅读这个csv文件吗 CSV输出 “^GSPC”,1403.36,“4/27/2012”,“下午4:32”,“3.381400.191406.641397.31574422720” “^IXIC”,3069.20,“2012年4月27日”,“下午5:30”,“+18.593060.343076.443043.30,0 ViewController.m 处理CSV解析比初始代码所暗示的要复杂得多,因为需要忽略带引号的分隔符。比如说, 1,"Hello, world!",2 有三列,但NS
处理CSV解析比初始代码所暗示的要复杂得多,因为需要忽略带引号的分隔符。比如说,
1,"Hello, world!",2
有三列,但NSScanner会找到四个标记(1
,“Hello
,“world!”,和2
)
要构建一个功能齐全的CSV解析器,有许多特殊情况需要处理,因此最快的方法是使用一个预构建的解析器。例如,您可以使用中描述的解析器。我回答这个问题可能为时已晚,但这个答案可能有助于人们轻松阅读CSV文件。这就是我不想阅读C的原因SV文件
- (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.
}
请尝试此方法:-(NSArray*)Components由字符串分隔:(NSString*)分隔符;请告诉我,如何将此文件内容作为字符串读取?“initWithContentsOfURL”在这里不起作用。
- (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.
}