I have a large CSV file. I want to start reading from line n. Currently I have the following code
CSV.foreach(path) do |row|
#process
end
I need to start reading from file n.
You can read specific rows using .readlines method:
require 'csv'
p CSV.readlines(path)[15..20] # array returned
# Benchmark
# user system total real
# 0.020000 0.000000 0.020000 ( 0.015769)
Other way(which, I believe, should not load entire file in memory):
from = 15
to = 20
csv = CSV.open(file, 'r')
# skipping rows before one we need
from.times { csv.readline }
# reading rows we need
(to - from).times { p csv.readline }
# Benchmark
# user system total real
# 0.000000 0.000000 0.000000 ( 0.000737)
i to j?
nCSV data or just a bunch of text?