0

I need to access individual lines in a CSV file with around 42,000 lines in ruby.

I'm aware of 'Foreach()' however, this is still too slow, and I need something that doesn't load the entire CSV to memory (I think this is what is slowing it down).

I was using Python and 'linecache.getline()' worked well, is there something similar in ruby? Thanks!

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
Joth
  • 13
  • 2
  • I made good experience with copying the data into a sqlite-DB and select from there. This may be a solution, if you have many access on an unchanged csv. – knut Aug 27 '20 at 09:41
  • Well, if you have to read arbitrary lines the file much more often than you add new ones there you could try the following: 1) read the file and build an index where for each line number you keep two numbers (starting point and length) and then 2) use lower level-machinery (`IO#seek`/`IO#pos`/`IO#read`) to position and read the necessary line(s) only without iteration over all of them. – Konstantin Strukov Aug 27 '20 at 12:59

0 Answers0