I have a form (Rails) which allows me to load a .csv file using the file_field
.
In the view:
<% form_for(:upcsv, :html => {:multipa
If your csv file doesn't fit to memory (can't use readlines), you can do:
def self.line_count(f)
i = 0
CSV.foreach(f) {|_| i += 1}
i
end
Unlike wc -l
this counts actual record count, not number of lines. These can be different if there are new lines in field values.
All of the solutions listed here actually load the entire file into memory in order to get the number of lines. If you're on a Unix-based system a much faster, easier and memory-efficient solution is:
`wc -l #{your_file_path}`.to_i
CSV.foreach(file_path, headers: true).count
Above will exclue header while counting rows
CSV.read(file_path).count
your_csv.count
should do the trick.
Just to demonstrate what IO#readlines does:
if you had a file like this: "asdflkjasdlkfjsdakf\n asdfjljdaslkdfjlsadjfasdflkj\n asldfjksdjfa\n"
in rails you'd do, say:
file = File.open(File.join(Rails.root, 'lib', 'file.json'))
lines_ary = IO.readlines(file)
lines_ary.count #=> 3
IO#readlines converts a file into an array of strings using the \n (newlines) as separators, much like commas so often do, so it's basically like
str.split(/\n/)
In fact, if you did
x = file.read
this
x.split(/\n/)
would do the same thing as file.readlines
** IO#readlines can be really handy when dealing with files which have a repeating line structure ("child_id", "parent_ary", "child_id", "parent_ary",...) etc
.length and .size are actually synonyms. to get the rowcount of the csv file you have to actually parse it. simply counting the newlines in the file won't work, because string fields in a csv can actually have linebreaks. a simple way to get the linecount would be:
CSV.read(params[:upcsv][:filename]).length