将远程csv加载到CHCSVParser [英] Load remote csv into CHCSVParser

查看:108
本文介绍了将远程csv加载到CHCSVParser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们。我使用Dave DeLong的CHCSVParser来解析csv。我可以在本地解析csv,但我不能得到它加载远程csv文件。我今天一直盯着我的MacBook的方式太长了,答案就在我面前。这是我的代码:

  NSString * urlStr = [[NSString alloc] initWithFormat:@http://www.somewhere.com /LunchSpecials.csv]; 
NSURL * lunchFileURL = [NSURL URLWithString:urlStr];
NSStringEncoding encoding = 0;
CHCSVParser * p = [[CHCSVParser alloc] initWithContentsOfCSVFile:[lunchFileURL path] usedEncoding:& encoding error:nil];
[p setParserDelegate:self];
[p parse];
[p release];感谢任何人可以给我的帮助。

=h2_lin>解决方案

- [NSURL path] 没有按照您的期望。



如果我有URL http://stackoverflow.com/questions/4636428 ,那么它的 -path / questions / 4636428 。当你将该路径传递给CHCSVParser时,它将尝试在本地系统上打开该路径。由于该文件不存在,您将无法打开它。



您需要做什么(Walter指出)是在本地下载CSV文件,然后打开它。您可以通过几种不同的方式下载文件( + [NSString stringWithContentsOfURL:...] NSURLConnection 等) 。一旦你有文件保存到本地磁盘或CSV内存中的字符串,然后可以将其传递给解析器。



如果这是一个非常大文件,那么你会想要 alloc / init a CHCSVParser 与CSV文件的本地副本的路径。然后解析器会逐位读取,并通过委托回调告诉你它找到了什么。



如果CSV文件不是很大,那么你可以做:

  NSString * csv = ...; // NSString包含CSV文件的内容
NSArray * rows = [csv CSVComponents];

这将返回 NSArray c $ c> NSArrays 的



NSArray 类别方法:

  NSString * csv = ...; 
NSError * error = nil;
NSArray * rows = [NSArray arrayWithContentsOfCSVString:csv encoding:[csv fastestEncoding] error:& error];

这将返回相同的结构( NSArray NSArrays of NSStrings ),但它也会为您提供一个 NSError object如果在CSV文件中遇到语法错误(即格式错误的CSV)。


Hey guys. I'm using Dave DeLong's CHCSVParser to parse a csv. I can parse the csv locally, but I cannot get it load a remote csv file. I have been staring at my MacBook way too long today and the answer is right in front of me. Here's my code:

NSString *urlStr = [[NSString alloc] initWithFormat:@"http://www.somewhere.com/LunchSpecials.csv"];
NSURL *lunchFileURL = [NSURL URLWithString:urlStr];
NSStringEncoding encoding = 0;
CHCSVParser *p = [[CHCSVParser alloc] initWithContentsOfCSVFile:[lunchFileURL path] usedEncoding:&encoding error:nil];
[p setParserDelegate:self];
[p parse];
[p release];

Thanks for any help that someone can give me.

解决方案

-[NSURL path] is not doing what you're expecting.

If I have the URL http://stackoverflow.com/questions/4636428, then it's -path is /questions/4636428. When you pass that path to CHCSVParser, it's going to try and open that path on the local system. Since that file doesn't exist, you won't be able to open it.

What you need to do (as Walter points out) is download the CSV file locally, and then open it. You can download the file in several different ways (+[NSString stringWithContentsOfURL:...], NSURLConnection, etc). Once you've got either the file saved locally to disk or the string of CSV in memory, you can then pass it to the parser.

If this is a very big file, then you'll want to alloc/init a CHCSVParser with the path to the local copy of the CSV file. The parser will then read through it bit by bit and tell you what it finds via the delegate callbacks.

If the CSV file isn't very big, then you can do:

NSString * csv = ...; //the NSString containing the contents of the CSV file
NSArray * rows = [csv CSVComponents];

That will return an NSArray of NSArrays of NSStrings.

Similar to this last approach is using the NSArray category method:

NSString * csv = ...;
NSError * error = nil;
NSArray * rows = [NSArray arrayWithContentsOfCSVString:csv encoding:[csv fastestEncoding] error:&error];

This will return the same structure (an NSArray of NSArrays of NSStrings), but it will also provide you with an NSError object if it encounters a syntax error in the CSV file (ie, malformed CSV).

这篇关于将远程csv加载到CHCSVParser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆