Count the length (number of lines) of a CSV file?

后端 未结 7 1183
野性不改
野性不改 2020-12-31 02:46

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         


        
相关标签:
7条回答
  • 2020-12-31 03:01

    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.

    0 讨论(0)
  • 2020-12-31 03:02

    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
    
    0 讨论(0)
  • 2020-12-31 03:05
    CSV.foreach(file_path, headers: true).count
    

    Above will exclue header while counting rows

    CSV.read(file_path).count
    
    0 讨论(0)
  • 2020-12-31 03:06

    your_csv.count should do the trick.

    0 讨论(0)
  • 2020-12-31 03:06

    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

    0 讨论(0)
  • 2020-12-31 03:14

    .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
    
    0 讨论(0)
提交回复
热议问题