计算 CSV 文件的长度(行数)?

发布于 2024-10-11 05:08:53 字数 1503 浏览 2 评论 0原文

我有一个表单(Rails),它允许我使用 file_field 加载 .csv 文件。 在视图中:

    <% form_for(:upcsv, :html => {:multipart => true}) do |f| %>
    <table>
        <tr>
            <td><%= f.label("File:") %></td>
            <td><%= f.file_field(:filename) %></td>
        </tr>
    </table>
        <%= f.submit("Submit") %>
    <% end %>

单击“提交”会将我重定向到另一个页面 (create.html.erb)。文件加载得很好,我能够在第二页中很好地阅读内容。我试图在第二页中显示 .csv 文件中的行数。

我的控制器(半伪代码):

class UpcsvController < ApplicationController
    def index
    end

    def create
        file = params[:upcsv][:filename]
        ...
        #params[:upcsv][:file_length] = file.length # Show number of lines in the file
        #params[:upcsv][:file_length] = file.size
        ...
    end
end

当我的文件仅包含 7 行时,file.lengthfile.size 都返回“91”。从我读到的 Rails 文档中,单击“提交”按钮后,Rails 会创建上传文件的临时文件,并且 params[:upcsv][:filename] 包含 temp/ 的内容上传的文件,而不是文件的路径。而且我不知道如何提取原始文件中的行数。获取文件行数的正确方法是什么?

我的 create.html.erb:

<table>
    <tr>
        <td>File length:</td>
        <td><%= params[:upcsv][:file_length] %></td>
    </tr>
</table>

我是 Rails 的新手(上周才开始),所以请忍受我的愚蠢问题。

谢谢你!

更新:显然数字“91”是我的文件中的单个字符数(包括回车符)。我的文件中的每一行都有 12 位数字 + 1 个换行符 = 13. 91/13 = 7。

I have a form (Rails) which allows me to load a .csv file using the file_field.
In the view:

    <% form_for(:upcsv, :html => {:multipart => true}) do |f| %>
    <table>
        <tr>
            <td><%= f.label("File:") %></td>
            <td><%= f.file_field(:filename) %></td>
        </tr>
    </table>
        <%= f.submit("Submit") %>
    <% end %>

Clicking Submit redirects me to another page (create.html.erb). The file was loaded fine, and I was able to read the contents just fine in this second page. I am trying to show the number of lines in the .csv file in this second page.

My controller (semi-pseudocode):

class UpcsvController < ApplicationController
    def index
    end

    def create
        file = params[:upcsv][:filename]
        ...
        #params[:upcsv][:file_length] = file.length # Show number of lines in the file
        #params[:upcsv][:file_length] = file.size
        ...
    end
end

Both file.length and file.size returns '91' when my file only contains 7 lines. From the Rails documentation that I read, once the Submit button is clicked, Rails creates a temp file of the uploaded file, and the params[:upcsv][:filename] contains the contents of the temp/uploaded file and not the path to the file. And I don't know how to extract the number of lines in my original file. What is the correct way to get the number of lines in the file?

My create.html.erb:

<table>
    <tr>
        <td>File length:</td>
        <td><%= params[:upcsv][:file_length] %></td>
    </tr>
</table>

I'm really new at Rails (just started last week), so please bear with my stupid questions.

Thank you!

Update: apparently that number '91' is the number of individual characters (including carriage return) in my file. Each line in my file has 12 digits + 1 newline = 13. 91/13 = 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

手心的海 2024-10-18 05:08:53

这里列出的所有解决方案实际上都是将整个文件加载到内存中以获得行数。如果您使用的是基于 Unix 的系统,更快、更简单且节省内存的解决方案是:

`wc -l #{your_file_path}`.to_i

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
满栀 2024-10-18 05:08:53

.length 和 .size 实际上是同义词。要获取 csv 文件的行数,您必须实际解析它。简单地计算文件中的换行符是行不通的,因为 csv 中的字符串字段实际上可以有换行符。获取行数的一个简单方法是:

CSV.read(params[:upcsv][:filename]).length

.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
方圜几里 2024-10-18 05:08:53

另一种读取行数的方法是

file.readlines.size

another way to read the number of lines is

file.readlines.size
孤独难免 2024-10-18 05:08:53
CSV.foreach(file_path, headers: true).count

上面将在计算行数时排除标题

CSV.read(file_path).count
CSV.foreach(file_path, headers: true).count

Above will exclue header while counting rows

CSV.read(file_path).count
放赐 2024-10-18 05:08:53

your_csv.count 应该可以解决问题。

your_csv.count should do the trick.

眼泪也成诗 2024-10-18 05:08:53

如果您的 csv 文件不适合内存(无法使用 readlines),您可以这样做:

def self.line_count(f)
  i = 0
  CSV.foreach(f) {|_| i += 1}
  i
end

与 wc -l 不同,它计算实际记录数,而不是行数。如果字段值中有新行,这些值可能会有所不同。

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.

疏忽 2024-10-18 05:08:53

只是为了演示 IO#readlines 的作用:

如果您有这样的文件:
"asdflkjasdlkfjsdakf\n
asdfjljdaslkdfjlsadjfasdflkj\n
asldfjksdjfa\n"

在 Rails 中你会这样做,说:

file = File.open(File.join(Rails.root, 'lib', 'file.json'))
lines_ary = IO.readlines(file)
lines_ary.count #=> 3

IO#readlines 使用 \n (换行符)作为分隔符将文件转换为字符串数组,就像逗号经常做的那样,所以它基本上就像

str.split(/\n/)

事实上,如果你 做

 x = file.read

这样

 x.split(/\n/)

会做与 file.readlines 相同的事情

** IO#readlines 在处理具有重复行结构(“child_id”,“parent_ary”,“child_id”,“parent_ary”,... ) ETC

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文