How do I use comma-separated-value (CSV) file received from a URL query in Objective-C? When I query the URL I get CSV data such as
"OMRUAH=X",20.741,"3/16/开发者_如何转开发2010","1:52pm",20.7226,20.7594
How do I parse and use this for my application?
My problem is creating/initializing that NSString
object in the first place. An example is this link http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv which returns CSV. I don't know how to parse this into an object since I cannot use an NSXMLParser
object.
You can try the following code using -componentsSeparatedByString:
which will end up with an NSArray of each component separated by:
NSURL *url = [NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv"];
NSString *reply = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
NSArray *csvItems = [reply componentsSeparatedByString:@","];
Claus
A couple of tools:
- http://michael.stapelberg.de/cCSVParse
- http://www.cocoadev.com/index.pl?ReadWriteCSVAndTSV
Here's an example of using NSScanner
: http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data
精彩评论