1

I have a file.csv file with over 180,000 lines in it. I need to pick out only about 8 lines from it. Each of these lines has go the same id so this is what the file would look like:

"id", "name", "subid"
"1", "Entry no 1", "4234"
"1", "Entry no 2", "5233"
"1", "Entry no 3", "2523"
. . . 
"1", "Entry no 8", "2322"
"2", "Entry no 1", "2344"

Is there a way for me to pick out just all the data with the id 1 or another numbers without indexing the whole file into a database (Either SQLITE or Core Data) since this would cause major performance issues for the app to have to index 180,0000 records. This is all for the iPhone and is on ios 5.

Thanks for the help.

Monolo
  • 18,205
  • 17
  • 69
  • 103
CoreCode
  • 2,227
  • 4
  • 22
  • 24

3 Answers3

2

Just parse the CSV and store the values in local variable. For parsing CSV via Objective-C checkout following tutorial(s):
http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data
http://cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html

Kind regards,
Bo

Bo.
  • 2,547
  • 3
  • 24
  • 36
  • Would that not create an incredibly large variable in ram? Also, would it be in an NSMutable Array? – CoreCode May 25 '12 at 12:02
  • You stated that you need only 8 lines to be stored. – Bo. May 25 '12 at 12:03
  • If you are concerned with reading large files following link should help you: http://stackoverflow.com/questions/1044334/objective-c-reading-a-file-line-by-line – Bo. May 25 '12 at 12:05
  • Yep but remember that the whole csv file has 180,000 lines in it so either I have to index it all and search for the ones with the id I want or I have to get the ones I need before anything. How would I only find and store the 8 lines I need from the entire 180,000 lined file? – CoreCode May 25 '12 at 12:05
  • 1
    You can hack any of the csv parsers such that it checks each line to see if the record you are looking for is there. – Bo. May 25 '12 at 12:19
2

I would strongly recommend putting that in Core Data, sure it will be indexed but that is actually a good thing since your lookups will be wayyy faster, parsing that document every time is going to be way more demanding than looking it up in Core Data, the overhead is a small price to pay.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • Actually, this software would have to download a new 180,000 lined file every day, delete the old database and parse the new file into a database. Also, the app will only ever use those 8 lines and never any other lines (The id of which lines are used is dependent on the person who downloads the app) – CoreCode May 25 '12 at 12:10
  • @CoreCode In that case it would be better to have that processing done in the sever side, I assume depending on the login credentials of the users they will get their 8 lines, if that is the case then it would be much better if the server did the lookup and return those 8 lines to the client. It would also be better for security and of course reduced network download time. – Oscar Gomez May 25 '12 at 12:13
  • Sadly, I do not have control over the server side of things. I only manage the app. – CoreCode May 25 '12 at 12:22
1

Sounds like a good job for Dave DeLong's CHCSVParser.

It works a bit like NSXMLParser, so you can just skip all the lines you don't want, and keep the 8 lines you do want.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Monolo
  • 18,205
  • 17
  • 69
  • 103