解析CSV文件时忽略标题行

时间:2021-11-10 13:39:13

How can the header line of the CSV file be ignored in ruby on rails while doing the CSV parsing!! Any ideas

在进行CSV解析时,如何在轨道上的ruby中忽略CSV文件的标题行!!有任何想法吗

6 个解决方案

#1


26  

If you're using ruby 1.8.X and FasterCSV, it has a 'headers' option:

如果你使用ruby 1.8.X和FasterCSV,它有一个'headers'选项:

csv = FasterCSV.parse(your_csv_file, {:headers => true}) #or false if you do want to read them

If you're using ruby 1.9.X, the default library is basically FasterCSV, so you can just do the following:

如果您使用的是ruby 1.9.X,则默认库基本上是FasterCSV,因此您可以执行以下操作:

csv = CSV.parse(your_csv_file, {headers: true})

#2


6  

csv = CSV.read("file")

csv.shift # <-- kick out the first line

csv # <-- the results that you want

#3


3  

A cool way to ignore the headers is to read it as an array and ignore the first row:

忽略标题的一种很酷的方法是将其作为数组读取并忽略第一行:

data = CSV.read("dataset.csv")[1 .. -1]
# => [["first_row", "with data"],
      ["second_row", "and more data"],
      ...
      ["last_row", "finally"]]

The problem with the :headers => false approach is that CSV won't try to read the first row as a header, but will consider it part of the data. So, basically, you have a useless first row.

:headers => false方法的问题是CSV不会尝试将第一行作为标题读取,但会将其视为数据的一部分。所以,基本上,你有一个无用的第一行。

#4


1  

I have found the solution to above question. Here is the way i have done it in ruby 1.9.X.

我找到了上述问题的解决方案。这是我在ruby 1.9.X中完成它的方式。

csv_contents = CSV.parse(File.read(file))
csv_contents.slice!(0)
csv=""
csv_contents.each do |content|
    csv<<CSV.generate_line(content)
end

#5


1  

Here is the simplest one worked for me. You can read a CSV file and ignore its first line which is the header or field names using headers: true:

这是最简单的一个适合我的方法。您可以读取CSV文件并忽略其第一行,即使用标题的标题或字段名称:true:

CSV.foreach(File.join(File.dirname(__FILE__), filepath), headers: true) do |row|
    puts row.inspect
end

You can do what ever you want with row. Don't forget headers: true

你可以用行做你想做的事。不要忘记标题:true

#6


0  

Easier way I have found is by doing this:

我找到的更容易的方法是这样做:

file = CSV.open('./tmp/sample_file.csv', { :headers => true })
# <#CSV io_type:File io_path:"./tmp/sample_file.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"" headers:true>

file.each do |row|
 puts row
end

#1


26  

If you're using ruby 1.8.X and FasterCSV, it has a 'headers' option:

如果你使用ruby 1.8.X和FasterCSV,它有一个'headers'选项:

csv = FasterCSV.parse(your_csv_file, {:headers => true}) #or false if you do want to read them

If you're using ruby 1.9.X, the default library is basically FasterCSV, so you can just do the following:

如果您使用的是ruby 1.9.X,则默认库基本上是FasterCSV,因此您可以执行以下操作:

csv = CSV.parse(your_csv_file, {headers: true})

#2


6  

csv = CSV.read("file")

csv.shift # <-- kick out the first line

csv # <-- the results that you want

#3


3  

A cool way to ignore the headers is to read it as an array and ignore the first row:

忽略标题的一种很酷的方法是将其作为数组读取并忽略第一行:

data = CSV.read("dataset.csv")[1 .. -1]
# => [["first_row", "with data"],
      ["second_row", "and more data"],
      ...
      ["last_row", "finally"]]

The problem with the :headers => false approach is that CSV won't try to read the first row as a header, but will consider it part of the data. So, basically, you have a useless first row.

:headers => false方法的问题是CSV不会尝试将第一行作为标题读取,但会将其视为数据的一部分。所以,基本上,你有一个无用的第一行。

#4


1  

I have found the solution to above question. Here is the way i have done it in ruby 1.9.X.

我找到了上述问题的解决方案。这是我在ruby 1.9.X中完成它的方式。

csv_contents = CSV.parse(File.read(file))
csv_contents.slice!(0)
csv=""
csv_contents.each do |content|
    csv<<CSV.generate_line(content)
end

#5


1  

Here is the simplest one worked for me. You can read a CSV file and ignore its first line which is the header or field names using headers: true:

这是最简单的一个适合我的方法。您可以读取CSV文件并忽略其第一行,即使用标题的标题或字段名称:true:

CSV.foreach(File.join(File.dirname(__FILE__), filepath), headers: true) do |row|
    puts row.inspect
end

You can do what ever you want with row. Don't forget headers: true

你可以用行做你想做的事。不要忘记标题:true

#6


0  

Easier way I have found is by doing this:

我找到的更容易的方法是这样做:

file = CSV.open('./tmp/sample_file.csv', { :headers => true })
# <#CSV io_type:File io_path:"./tmp/sample_file.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"" headers:true>

file.each do |row|
 puts row
end