4

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.

3
  • Are the lines before n CSV data or just a bunch of text? Commented Nov 15, 2014 at 20:48
  • CSV they are CSV records Commented Nov 15, 2014 at 20:53
  • have the lines (or columns) always the same length or may it differ? And remember, with UTF-8 the bytes may differ, even if there a similar numbers of characters. Commented Nov 15, 2014 at 21:02

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

It is large file >10gb wouldnt readlines hog the entire memory?
@yatishmehta do you know specific lines which you want to read? I mean from i to j?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.