我有一个很大的CSV文件,想要从第n行开始读取。目前我有以下代码:
CSV.foreach(path) do |row|
#process
end
我需要从文件n开始阅读。
require 'csv'
p CSV.readlines(path)[15..20] # array returned
# Benchmark
# user system total real
# 0.020000 0.000000 0.020000 ( 0.015769)
另一种方式(我相信不应该在内存中加载整个文件):
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
到 j
? - Rustam Gasanov